* realloc array of structures
@ 2004-04-25 16:16 J.
0 siblings, 0 replies; 2+ messages in thread
From: J. @ 2004-04-25 16:16 UTC (permalink / raw)
To: linux-c-programming
Sunday, April 25 18:08:29
Hello,
I am trying to put incomming data into an array of structures. For example
everytime a new line arrives, grow the array by one, and copy the line
into the newly allocated mem structure.
Unfortunatly I am a bit lost in the pointer world. Could someone give me
some hints what to what goes wrong in the small example code below ?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
char *str;
};
int main(void) {
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 *));
/* 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;
}
/* any comments or suggestions are more then welcome.. Thnkx ! */
/* J. */
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: realloc array of structures
@ 2004-04-25 17:28 Jad Saklawi
0 siblings, 0 replies; 2+ messages in thread
From: Jad Saklawi @ 2004-04-25 17:28 UTC (permalink / raw)
To: J.; +Cc: linux-c-programming
Quoting "J." <mailing-lists@xs4all.nl>:
> Sunday, April 25 18:08:29
>
> Hello,
>
> I am trying to put incomming data into an array of structures. For example
> everytime a new line arrives, grow the array by one, and copy the line
> into the newly allocated mem structure.
>
> Unfortunatly I am a bit lost in the pointer world. Could someone give me
> some hints what to what goes wrong in the small example code below ?
Here is a fixed version of your code :
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
char *str;
};
int main(void) {
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[count] = (struct node *)malloc(sizeof(struct node));
/* copy the line to member str of the new element (structure) */
strarray[count]->str = strdup(line);
count++;
}
for(i = 0; i < count; i++)
printf("[%d].str: %s", i, strarray[i]->str);
return 0;
}
You needed to allocate memory for each struct, and you should use -> when
using struct pointers instead of .
Greets,
Jad Saklawi
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2004-04-25 17:28 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-04-25 16:16 realloc array of structures J.
-- strict thread matches above, loose matches on Subject: below --
2004-04-25 17:28 Jad Saklawi
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).