linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "J." <mailing-lists@xs4all.nl>
To: linux-c-programming@vger.kernel.org
Subject: Re: realloc array of structures, solved!
Date: Sun, 25 Apr 2004 19:11:35 +0200 (CEST)	[thread overview]
Message-ID: <Pine.LNX.4.21.0404251906450.10171-100000@hestia> (raw)
In-Reply-To: <Pine.WNT.4.58.0404252206330.1664@umas>

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;
> > }


           reply	other threads:[~2004-04-25 17:11 UTC|newest]

Thread overview: expand[flat|nested]  mbox.gz  Atom feed
 [parent not found: <Pine.WNT.4.58.0404252206330.1664@umas>]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.21.0404251906450.10171-100000@hestia \
    --to=mailing-lists@xs4all.nl \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).