www.delorie.com/archives/browse.cgi | search |
X-Authentication-Warning: | delorie.com: mail set sender to djgpp-bounces using -f |
From: | Martin Ambuhl <mambuhl AT earthlink DOT net> |
User-Agent: | Mozilla Thunderbird 1.0.2 (Windows/20050317) |
X-Accept-Language: | en-us, en |
MIME-Version: | 1.0 |
Newsgroups: | comp.os.msdos.djgpp |
Subject: | Re: pointers in Strucrure assignment problem |
References: | <1120713425 DOT 526371 DOT 222910 AT g47g2000cwa DOT googlegroups DOT com> |
In-Reply-To: | <1120713425.526371.222910@g47g2000cwa.googlegroups.com> |
Lines: | 48 |
Message-ID: | <TG4ze.17390$pa3.15032@newsread2.news.atl.earthlink.net> |
Date: | Thu, 07 Jul 2005 07:18:11 GMT |
NNTP-Posting-Host: | 165.247.43.38 |
X-Complaints-To: | abuse AT earthlink DOT net |
X-Trace: | newsread2.news.atl.earthlink.net 1120720691 165.247.43.38 (Thu, 07 Jul 2005 00:18:11 PDT) |
NNTP-Posting-Date: | Thu, 07 Jul 2005 00:18:11 PDT |
Organization: | EarthLink Inc. -- http://www.EarthLink.net |
To: | djgpp AT delorie DOT com |
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp |
Reply-To: | djgpp AT delorie DOT com |
aveo wrote: > Hi all, > Output of following program is unexpected. > It affecting all elements of LD insted of 0th element > Plz help me out. [...] > typedef struct LineDate > { > unsigned char *LinePtr; > int LineCount; > }LD1; > > LD1 LD[100]; > > void main(void) main returns an int in a hosted environment for either C or C++. > { > int i; > //Intitialize LineData Structure---->>>>> > for(i=0;i<=99;i++) > { > LD[i].LinePtr = (char *)calloc( 100, sizeof(char) ); sizeof(char) is 1 by definition, and, if this is C, the cast serves only to mask the error of not #including <stdlib.h>. You should also test for the success of calloc. > LD[i].LinePtr=""; The second of these causes you to 1) lose the value returned by calloc 2) create a memory leak 3) assign LD[i].LinePtr to point to the string literal "", Further, since you used calloc, the strings are set to be empty anyway, so the seconde line is pointless. A more efficient approach is #define STRSIZE 100 if (!(LD[i].LinePtr = malloc(STRSIZE))) { /* handle error */ } *LD[i].LinePtr = 0;
webmaster | delorie software privacy |
Copyright © 2019 by DJ Delorie | Updated Jul 2019 |