delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/01/20/23:16:13

Date: Mon, 20 Jan 1997 23:03:22 -0500 (EST)
From: Michael Phelps <morphine AT hops DOT cs DOT jhu DOT edu>
To: Josh Anderson <randerso AT win DOT bright DOT net>
cc: djgpp AT delorie DOT com
Subject: Re: Dynamic Arrays
In-Reply-To: <01bc072e$33eb9b20$ee038cd0@wahander>
Message-ID: <Pine.GSO.3.95.970120230057.8156A-100000@hops.cs.jhu.edu>
MIME-Version: 1.0

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019