Path: utzoo!utgpu!utstat!jarvis.csri.toronto.edu!mailrus!uflorida!ukma!xanth!mcnc!duke!romeo!khera From: khera@romeo.cs.duke.edu (Vick Khera) Newsgroups: comp.lang.c Subject: Re: malloced structure initilization Message-ID: <13480@duke.cs.duke.edu> Date: 10 Feb 89 00:15:02 GMT References: <202@sabin.UUCP> Sender: news@duke.cs.duke.edu Reply-To: khera@cs.duke.edu (Vick Khera) Organization: Duke University CS Dept.; Durham, NC Lines: 30 In article <202@sabin.UUCP> bjm@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@cs.duke.edu Department of Computer Science CSNET: khera@duke Duke University UUCP: decvax!duke!khera Durham, NC 27706