From hyperc@calvin.sfasu.EDU Mon May 18 23:02:38 1992 Received: from calvin.sfasu.edu by umaxc.weeg.uiowa.edu (5.61.jnf/920125) on Mon, 18 May 92 23:02:35 -0500 id AA04247 with SMTP Received: by calvin.sfasu.EDU (5.59/25-eef) id AA16642; Mon, 18 May 92 16:42:30 CDT Return-Path: Message-Id: <9205182142.AA16642@calvin.sfasu.EDU> From: calvin!hyperc@calvin.sfasu.EDU (Hyperc list account) X-Mailer: SCO System V Mail (version 3.2) To: hyperc-l@calvin.sfasu.EDU Subject: HyperC and arrays Date: Mon, 18 May 92 16:42:30 CDT Status: R From ST2975@SIUCVMB.SIU.EDU Sun May 17 19:18:56 1992 Received: from SIUCVMB.SIU.EDU by calvin.sfasu.EDU with SMTP (5.59/25-eef) id AA14903; Sun, 17 May 92 19:18:56 CDT Return-Path: Message-Id: <9205180018.AA14903@calvin.sfasu.EDU> Received: from SIUCVMB.SIU.EDU by SIUCVMB.SIU.EDU (IBM VM SMTP V2R1) with BSMTP id 6642; Sun, 17 May 92 19:27:49 CST Date: Sun, 17 May 92 19:27:15 CST From: ST2975@SIUCVMB.SIU.EDU To: hyperc-l@calvin.sfasu.edu Subject: HyperC and arrays Status: O In experimenting with HyperC, I'd like to ask a question: In mixing arrays with structs, I found that I could not introduce an array of 1 element such as array[0] or for that matter declare an array of 1 element but must sub- script with at least an integer of 1. In testing this, I used the sizeof operator to measure the size of various elements. Following is the source and output file: ============================================================================== /* get file info test */ #include typedef struct getfileinfo { char param_count[1]; char pathname[2]; char access[1]; char file_type[1]; char aux_type[2]; char storage_type[1]; int blocks_used; /* int mod_date; int mod_time; int create_date; int create_time; */ double time; } getfileinfo; main() { getfileinfo gfi; char CHR[1]; /* must be declared with at least 1 as a subscript */ int INTGR[1]; /* ditto... */ FILE fi; fi = create("test"); open(fi); fprintf(fi,"\n size in bytes of struct gfi and its parts: \n \ struct: %d \n \ param_count: %d \n \ pathname: %d \n \ access: %d \n \ file_type: %d \n \ aux_type: %d \n \ storage_type: %d \n \ blocks_used: %d \n \ /* mod_date: %d \n \ mod_time: %d \n \ create_date: %d \n \ create_time: %d \n", */ time: %d \n", sizeof(gfi), sizeof(gfi.param_count), sizeof(gfi.pathname), sizeof(gfi.access), sizeof(gfi.file_type), sizeof(gfi.aux_type), sizeof(gfi.storage_type), sizeof(gfi.blocks_used),/*sizeof(gfi.mod_date), sizeof(gfi.mod_time), sizeof(gfi.create_date), sizeof(gfi.create_time)*/ sizeof(gfi.time) ); fprintf(fi,"\nsize in bytes of array CHR[0]: %d\n",sizeof(CHR[0])); fprintf(fi,"\nsize in bytes of array INTGR[0]: %d\n",sizeof(INTGR[0])); close(fi); } ============================================================================== size in bytes of struct gfi and its parts: struct: 18 param_count: 1 pathname: 2 access: 1 file_type: 1 aux_type: 2 storage_type: 1 blocks_used: 2 time: 8 size in bytes of array CHR[0]: 1 size in bytes of array INTGR[0]: 2 ============================================================================== Would anyone like to comment on this? Is this a bug, feature, ...? Andy. From @mail.uunet.ca:beejay@micor Thu Jun 4 04:52:15 1992 Received: from calvin.sfasu.edu by umaxc.weeg.uiowa.edu (5.61.jnf/920526) on Thu, 4 Jun 92 04:51:45 -0500 id AA20585 with SMTP Received: from mail.uunet.ca (uunet.ca) by calvin.sfasu.EDU with SMTP (5.59/25-eef) id AA07782; Thu, 4 Jun 92 04:22:28 CDT Return-Path: <@mail.uunet.ca:beejay@micor> Received: from ocunix by mail.uunet.ca with UUCP id <10058>; Thu, 4 Jun 1992 05:32:58 -0400 Received: by micor.OCUnix.On.Ca (smail2.5) id AA15469; 3 Jun 92 19:52:57 EDT (Wed) To: hyperc-l@calvin.sfasu.edu Subject: sizeof() Date: Wed, 3 Jun 1992 15:52:57 -0400 Message-Id: <9206031952.AA15467@micor.OCUnix.On.Ca> From: beejay@micor.ocunix.on.ca (Basil Johnson) Status: R mdj@pro-mdj.cts.com (Mark de Jong) writes: >Ah, but I _have_ declared the number of elements. By doing: > > char *names[] = { > "Mark", > "Basil" > }; > > I have _explicitly_ stated that the array has 2 elements that are pointers to > two specific strings, "Mark" and "Basil". I would say the declaration is _implicit_, not _explicit_ but I won't quibble. I don't have access to other C compilers, so if sizeof() works on others even when the number of elements have not been explicitly declared then it may very well be that you have discovered a quirk in HyperC. Note in the demo below, that when the number of elements in the array is _explicitly_ stated (names2), then it is possible to compute the number of elements in the manner you wanted to. It's not included in the demo, but if you change the class of names2 from static to auto, sizeof() returns the same value. So, it seems that sizeof() doesn't work only when we don't have an explicit declaration. Try this: main() { char names1[3]; static char *names2[2]; /* explicit declaration */ static char *names3[] = {"Tom", "Dick"}; /* implicit declaration */ int i; int *names_ptr; names2[0] = "John"; names2[1] = "Jane"; names_ptr = &names3; printf("names1 is at $%x and is %d bytes long\n", names1, sizeof(names1)); printf("names2 is at $%x and is %d bytes long\n", names2, sizeof(names2)); printf("names3 is at $%x and is %d bytes long\n", names3, sizeof(names3)); for (i = 0; i < 2; i++) { printf("element %d of names3 is at $%x and holds $%x\n", i, names_ptr, *names_ptr); names_ptr++; } printf("\nNumber of elements in names2 is %x\n", (sizeof(names2)/ sizeof(names2[0]))); } I know you weren't interested in the actual length of each string. As I said, It was just a remnant of some test code that I didn't bother to remove. > Anyway, thanks for your input. You have a verr good grasp on C, even though > you keep saying you're a beginner. I'm beginning to wonder if you _really_ > are a beginner! :-) Thanks. I really am a beginner at programming in C, but I have read a number of articles to get a flavour for the language before I got HyperC. The only C programs I have written are the ones that conversations via this mailing list has encouraged me to research and try. Maybe it's true that assembly language programmers pick up C faster than others..... I read that somewhere! Basil Basil Johnson UUCP: micor!beejay |Oh, Fate! ... Nepean, Ontario InterNet: |Such a stupid thing: CANADA K2B 8E9 beejay@micor.ocunix.on.ca |Command a bird to fly (613) 820-0804 |Then clip its wing! _