Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!cs.utexas.edu!sun-barr!olivea!uunet!garfield!jupiter!d0np From: d0np@jupiter.sun.csd.unb.ca (GEMMELL) Newsgroups: comp.lang.c Subject: Re: Variable record size -- ANSI Message-ID: <1991Apr12.021405.12054@jupiter.sun.csd.unb.ca> Date: 12 Apr 91 02:14:05 GMT Organization: University of New Brunswick Lines: 41 + Michael N Johnston writes: + ]>struct foo { + ]>unsigned short recsize; + ]>unsigned short num; + ]>char info [24]; + ]>byte flags; + ]>char filename[1] + ]>}; + ]>Where, foo.filename is a placeholder for a variable length string. + ]If you plan to store a char array in filename DON'T. Use char *filename + ]and point this to your actual array. Using your origional declaration + ]and doing something like strcpy(foo_ptr->filename, "abcde") will lead + ]to disaster. + Why should this lead to disaster? Is this not allowed by ANSI? + If you can make sure that you have malloced more than enough space for the + structure, shouldn't you be able to use all this space by doing just that? It would not lead to disaster if you do malloc enough space, but I don't think doing a malloc like t = (struct foo *)malloc( sizeof( struct foo ) + 10 (or + var, etc) ); would be all that useful. If you want to increase the size of your string you would have to reallocate a new structure, copy over the old data. I agree with the use of char *filename, it would require a little less overhead, if you just have to allocate or reallocate space for a string. -------------------------------------------- | | | \ \ \ \ \ /\ d0np | | / / / / / || @unb.ca OR | | \ \ \ \ \ || @jupiter.sun.csd.unb.ca | | / / / / / || Michael G. Gemmell | | \ \ \ \ \ || | | / / / / / ------ I hereby disclaim | | \ \ \ \ \ () that my opinions are | | / / / / / () soley my own, I'm sure | | nobody else would want them | --------------------------------------------