Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!linus!philabs!cmcl2!seismo!umcp-cs!chris From: chris@umcp-cs.UUCP (Chris Torek) Newsgroups: net.lang.c Subject: Re: Declaring pointer to array of structures, within a structure Message-ID: <1792@umcp-cs.UUCP> Date: Tue, 3-Jun-86 19:47:54 EDT Article-I.D.: umcp-cs.1792 Posted: Tue Jun 3 19:47:54 1986 Date-Received: Thu, 5-Jun-86 07:27:06 EDT References: <909@ttrdc.UUCP> Reply-To: chris@maryland.UUCP (Chris Torek) Organization: University of Maryland, Dept. of Computer Sci. Lines: 69 In article <909@ttrdc.UUCP> levy@ttrdc.UUCP (Daniel R. Levy) writes: >typedef struct bar { > char *c; > struct foo *d; /* Is this right, to point to an ARRAY of struct foos, > please read on */ >}; You left out the type name for the typedef, but other than that, yes, it is correct. >... But what bugs me is that in my typedef of bar, I don't say >that member d is a pointer to an ARRAY of struct foo, I just say >that it is a pointer to one struct foo. Is this kosher, or have >I violated some subtle but important distinction between arrays >and pointers to them? It is fine. All a compiler needs to implement an unchecked one-dimensional array is a base address and an element size; and a pointer to any `object with size' supplies both. >Remember, I'd like to be able to treat member d as I would the >name of any other array in referencing the memory it points to. The clause `in referencing ...' is important. Given (e.g.) struct bar bar_array[10]; the compiler `knows' how many `bar_array' elements there are. `sizeof bar_array' returns the number of bytes occupied by the entire array; and a compiler could insert subscript checking on accesses to bar_array[i]. However, given struct bar *bar_pointer; the compiler only `knows' that `bar_pointer' is of type `pointer to struct bar' and that `*bar_pointer' is of type `struct bar'; `sizeof bar_pointer' gives the number of bytes occupied by the pointer, and a compiler is much harder pressed to come up with `subscript' checking for bar_pointer[i] (though it *can* be done). >When I tried to say d is a pointer to an array of struct foo and tried: > > struct foo *(d[]); > or > struct foo (*d)[]; The second is a `pointer to array of struct foo' in cdecl syntax. (The first is `array of pointer to struct foo', not what you asked for.) In the latter case case the array part carries no number of elements, which creates a bit of a problem if one writes `d[i]': to find d[i], the compiler must go to the i'th `d', but how big is each d[j], 0 <= j < i? (Answer: somewhere between zero `struct foo's and an infinite number of `struct foo's. This is no help.) A compiler could still handle (*d)[i], though: no matter how big (*d) is, its address is zero bytes away from the address of (*d); and (*d)[0] is `sizeof (struct foo)' bytes big. Zero times anything is zero, so the size of d[0], a.k.a. (*d), is unimportant. (Actually, I have glossed over something. The size of (*d) must be known if different pointers are different sizes, else how is the compiler to generate the correct dereference instruction? On most machines all pointers are the same size, and one can get away with a generic dereference instruction. In general, the more information you feed your compiler---assuming it is correct---the better.) -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1516) UUCP: seismo!umcp-cs!chris CSNet: chris@umcp-cs ARPA: chris@mimsy.umd.edu