Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!bloom-beacon!adam.pika.mit.edu!scs From: scs@adam.pika.mit.edu (Steve Summit) Newsgroups: comp.lang.c Subject: Re: C pointers Summary: dynamically allocated arrays of structures Keywords: C array struct malloc Message-ID: <9234@bloom-beacon.MIT.EDU> Date: 13 Feb 89 06:03:32 GMT References: <1707@shaffer.UUCP> Sender: daemon@bloom-beacon.MIT.EDU Reply-To: scs@adam.pika.mit.edu (Steve Summit) Lines: 75 In article <1707@shaffer.UUCP> jcrowe@shaffer.UUCP (Joe Crowe) writes: >I have a structure which contains among >other things a pointer to an array of structures declared as follows: >typedef struct XYZ >{ > struct diag_cmd (*p_cmd_blk)[]; >} t_xyz; > > I am allocating storage for the above structures dynamically and >setting the pointer p_cmd_blk manually. All of this works. While your declaration of a pointer to an array of structures is correct, it has two problems: 1. The array size isn't specified, making this pointer even less useful than most pointers to arrays. 2. It probably isn't what you want. I suspect that the intent was that an XYZ structure contain an arbitrary number of diag_cmd structures, with the number unknown at compile time. That is, you wanted something like struct diag_cmd p_cmd_blk[n]; where n is determined at run time (n is probably another member of the XYZ structure). What you want is struct diag_cmd *p_cmd_blk; which can be initialized with t_xyz.p_cmd_blk = (struct diag_cmd *)malloc(n * sizeof(struct diag_cmd)); and dereferenced with t_xyz.p_cmd_blk[i].cmd_blk_field The key is that a pointer to a structure (or any kind of pointer, for that matter) need not point to only one instance of the structure. In this case, it "points at" an "array" of n structures, although the pointer's type is "pointer to structure" rather than "pointer to array of structure." For the usual cases, "pointer to struct" is in fact preferable; pointer arithmetic (and, by extension, array-like subscripting) works as you probably want it to: selecting among the various single structures in the "array" of n of them. (In this paragraph, I have used the terms "points at" and "array", in quotes, in a loose way -- struct diag_cmd *p_cmd_blk neither declares an array, nor, formally, points at one, but once allocated, it acts like an array for all practical purposes.) "pointer to array of struct" would, on the other hand, increment or index over entire arrays of structures at once, which is why it's a fairly useless concept if the size of the array is unspecified. (Many compilers get pointers to arrays wrong, which is both unsurprising, since they're so confusing, and benign, since they're so rarely useful. I suspect that "pointer to array" with the size of the array unspecified should elicit a warning or error message. pcc apparently pretends that the size is zero, because incrementing the pointer adds 0 to it. My C interpreter says "missing array dimension.") Although true pointers to arrays could be made to work in this example, they would require extradereferencing and punctuation, and generally only confuse the issue. Steve Summit scs@adam.pika.mit.edu P.S. This example helps to illustrate why, if something of type "array of X" appears in a C expression, it is converted into a pointer with type "pointer to X" (rather than "pointer to array of X"). The resultant pointer "acts like" the original array; i.e. after saying p=a, p[i] references a[i].