linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Re: realloc array of structures, solved!
       [not found] <Pine.WNT.4.58.0404252206330.1664@umas>
@ 2004-04-25 17:11 ` J.
  0 siblings, 0 replies; only message in thread
From: J. @ 2004-04-25 17:11 UTC (permalink / raw)
  To: linux-c-programming

Sunday, April 25 19:06:45

Thanks [Uma] for the quick reply, I can see now what I did wrong and
very important; why my code wasn't working.

Thnkx again..

J.

> On Sun, 25 Apr 2004, Uma Shankar Kayarohanam wrote:
> > #include <stdio.h>
> > #include <string.h>
> > #include <stdlib.h>
> >
> > struct node {
> >  char *str;
> > };
> >
> > int main(void) {
> >  struct node **strarray = NULL;
> 
> struct node *strarray = NULL;
> 
> >  int i = 0, count = 0;
> >  char line[1024];
> >
> >  while(fgets(line, 1024, stdin) != NULL) {
> >   /* grow the array with one element */
> >   strarray = (struct node **)realloc(strarray, (count + 1) * sizeof(struct node *));
> 
> strarray = (struct node *)realloc(strarray, (count + 1) * sizeof(struct node *));
> 
> >   /* copy the line to member str of the new element (structure) */
> >   strarray[count++].str = strdup(line);
> >  }
> >
> >  for(i = 0; i < count; i++)
> >   printf("[%d].str: %s", i, strarray[i].str);
> >
> >  return 0;
> > }


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2004-04-25 17:11 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <Pine.WNT.4.58.0404252206330.1664@umas>
2004-04-25 17:11 ` realloc array of structures, solved! J.

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).