Mail Archives: djgpp/1997/01/20/23:16:13
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
- Raw text -