malloced structure initilization

Vick Khera khera at romeo.cs.duke.edu
Fri Feb 10 10:15:02 AEST 1989


In article <202 at sabin.UUCP> bjm at sabin.UUCP (Brendan J. McMahon) writes:
>How do you initialize memory malloced for a structure without using calloc,
>and without initilizing each structure element explicitly?
>
>struct foo{
>    int a;
>    long b;
>    char c[80];
>    double d;
>};
 [..... some code omitted ...]
>func(fooptr)   struct foo *fooptr;
>{   struct foo *fp2;
>    for(fp2=fooptr; fp2 < fooptr + sizeof(struct foo);  fp2++)
							 ^^^^^^
this statement increments fp2 to point to the next struct foo, not to the
next byte in struct foo as pointed to by fp2, as this example seems to
want. why not just use
 bzero((char *)fooptr, sizeof(struct foo))
to zero out the structure.  besides, what's wrong with calloc()? have I
missed some point you were trying to make here?

>             *fp2=NULL;  /* Error - incompatible types */
>}


=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
ARPA:	khera at cs.duke.edu		Department of Computer Science
CSNET:	khera at duke        		Duke University
UUCP:	decvax!duke!khera		Durham, NC 27706



More information about the Comp.lang.c mailing list