Date: Mon, 20 Jan 1997 23:03:22 -0500 (EST) From: Michael Phelps To: Josh Anderson cc: djgpp AT delorie DOT com Subject: Re: Dynamic Arrays In-Reply-To: <01bc072e$33eb9b20$ee038cd0@wahander> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII On 21 Jan 1997, Josh Anderson wrote: > I know this is a stupid question, but how do you create a dynamic array of > a bunch of structs? I only need to set the size of the array at init time. > And then free the memory at exit. > > Thanks in advance, > Josh > Simple. struct test { char a, b, c; }; int main() { struct test *my_array; my_array = (struct test *)malloc(50 * sizeof(struct test)); if (my_array == NULL) { fprintf(stderr, "Unable to allocate sufficient memory.\n"); exit(1); } /* put your code here */ free(my_array); /* if necessary--done automatically on exit */ return 0; } ---Michael Phelps morphine AT cs DOT jhu DOT edu CH3 | N / | ______/ | / \ CH2 _____/ \__|__ // \\ / | \\ // \\______/___CH2 \\ \ / \ / \______/ \_____/ / ------ \ / \ OH \ / OH O Morphine