Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!cmcl2!beta!dph From: dph@beta.UUCP (David P Huelsbeck) Newsgroups: comp.lang.c Subject: Re: Arrays of Unknown Length in Structures (and sizeof) Message-ID: <10760@beta.UUCP> Date: Thu, 1-Oct-87 11:29:20 EDT Article-I.D.: beta.10760 Posted: Thu Oct 1 11:29:20 1987 Date-Received: Mon, 5-Oct-87 03:43:40 EDT References: <243@mit-prep.ARPA> <1044@ius1.cs.cmu.edu> <786@cc5.bbn.com.BBN.COM> Reply-To: dph@LANL.GOV.ARPA (David P Huelsbeck) Organization: Los Alamos Natl Lab, Los Alamos, N.M. Lines: 69 Summary: array[0] ? (really?) In article <786@cc5.bbn.com.BBN.COM> keesan@bbn.com (Morris M. Keesan) writes: >In article <1044@ius1.cs.cmu.edu> edw@ius1.cs.cmu.edu (Eddie Wyatt) writes: >>In article someone writes: >>> struct STRUCT_FOO >>> { >>> int one; >>> int two; >>> int open[]; >>> }; >>> [... some code using the above deleted ...] >> Let me give you a reason of why this should not be "kosher". [... some reasons deleted ...] >The reason the above is not kosher is that int open[] declares an array of >unknown length, and therefore the enclosing structure is of unknown length and >sizeof(struct STRUCT_FOO) is undefined. However, given the above citation from >K&R, I think the example would be completely kosher if "int open[]" were >replaced with "int open[0]". There's nothing forbidding zero-sized arrays. ^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^ >-- >Morris M. Keesan >keesan@bbn.com >{harvard,decvax,ihnp4,etc.}!bbn!keesan Hmmm? I'm trying to imagine what possible semantics could be associated with a zero-sized array. An array name is a constant that contains the starting address of some pre-allocated space in memory. In other words it's a pointer constant. What value could the compiler assign that would point to zero pre-allocated memory locations? NULL? And what good would it do you to have this constant? I think the idea here was to declare a structure that could be an unkown total size. this would be a nice thing to do when declaring something like a functions activation record to be placed on the stack. You know you're going to have a static-link a dynamic-link and most likely some arguments. But how may args, and of what size? So you want a structure with the "bottom" left open until latter. I think what you need is to declare a structure like: typedef struct FOO { int *open; int one; int two; int open0; } foo; Then when you know that you'll need "x" elements in "open" you: foop = (foo *) malloc(sizeof(foo) + (x - 1)); foop->open = &open0; for (i=0; i < x; i++) { foop->open[i] = i; /* or whatever */ } Is this what the original poster intended? I'll admit it looks pretty sick but I think it'd work. How 'bout it C hacks? Is there a better way to do this? Will this even work? I'm sorry I don't have the time to check just now. David Huelsbeck dph@lanl.gov.arpa {cmcl2!ihnp4}!lanl!dph