malloced structure initilization

Brendan J. McMahon bjm at sabin.UUCP
Thu Feb 9 02:09:19 AEST 1989


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

#include <stdio.h>
#include <malloc.h>

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

main()
{
    struct foo *fooptr;

    if ( (fooptr=(struct foo *)malloc(sizeof(struct foo))) == NULL){
              puts("Malloc Bombed");
              exit(-1);
    }

    func(fooptr);
}

func(fooptr)
    struct foo *fooptr;
{
    struct foo *fp2;

    for(fp2=fooptr; fp2 < fooptr + sizeof(struct foo);  fp2++)
             *fp2=NULL;  /* Error - incompatible types */

}



More information about the Comp.lang.c mailing list