malloced structure initilization

Andrew Koenig ark at alice.UUCP
Fri Feb 10 15:44:51 AEST 1989


In article <202 at sabin.UUCP>, bjm at sabin.UUCP (Brendan J. McMahon) writes:

> Simple problem - 
> How do you initialize memory malloced for a structure without using calloc,
> and without initilizing each structure element explicitly?
> Example --

> #include <malloc.h>

> struct foo{
>     int a;
>     long b;
>     char c[80];
>     double d;
> };

How about this?

	static struct foo init_foo;
	struct foo *fp;
	fp = (struct foo *) malloc (sizeof (struct foo));
	*fp = init_foo;

init_foo is guaranteed to be initialized because it's static.
-- 
				--Andrew Koenig
				  ark at europa.att.com



More information about the Comp.lang.c mailing list