Path: utzoo!attcan!uunet!ncrlnk!ncrcae!ece-csc!ncsuvx!gatech!rutgers!att!ulysses!andante!alice!ark From: ark@alice.UUCP (Andrew Koenig) Newsgroups: comp.lang.c Subject: Re: Dynamically malloc()-able pointers in a struct? Message-ID: <8597@alice.UUCP> Date: 24 Dec 88 14:41:23 GMT References: <448@blake.acs.washington.edu> Distribution: na Organization: AT&T Bell Laboratories, Liberty Corner NJ Lines: 32 In article <448@blake.acs.washington.edu>, phaedra@blake.acs.washington.edu (J. Anderson) writes: > struct xyzzy { > int plugh; > long black_rod; > char *foobar; /* This wants to be a pointer to a variable-length > * array > */ > } Nothing particularly hard about this, except that you must allocate two separate areas of memory, one for an `xyzzy' object and one for the character array addressed by `foobar'. The `xyzzy' object may be a local variable, of course: struct xyzzy a; a.foobar = malloc(n); If you want the `xyzzy' object to be dynamic as well, do this: struct xyzzy *p; p = (struct xyzzy *) malloc (sizeof(struct xyzzy)); p->foobar = malloc(n); To free it, reverse the process: free(p->foobar); free(p); What's the problem? -- --Andrew Koenig ark@europa.att.com