|
|
|
|
| View previous topic :: View next topic |
| Author |
Message |
cloudRunner
Daedalian Member
|
Posted: Wed Jul 04, 2007 5:16 pm Post subject: 1 |
|
|
I've been working on a program that involves the use of an array of structs (not pointer-based). The follow is the declaration of the array:
| Code: |
typedef struct
{
char fullname[15];
int age;
float weight;
} person;
person list[10]; //creates 10-element array
int size=0; //array size counter
|
I go on to read data from a file. As each line of the file is read, I print each item. Everything prints out fine and dandy.
However, when I try storing them in the array and printing out the contents of the array, I get 0's and nulls. Here's the rest of the code:
| Code: |
while((fscanf(file, "%s %d %f",name, &x, &y)!= EOF) && size<10)
{
printf("%s %d %f\n",name,x,y); //everything prints out fine!
strcpy(list[size].fullname,name);
list[size].age=x;
list[size].weight=y; // yet, NONE of this stuff is stored in array
size++;
}
|
I have a feeling that the reason the array is empty is because there's something off with the declaration of the array. Can anyone see what I'm doing wrong? _________________ Forever is such an unpleasant word |
|
| Back to top |
|
 |
Bicho the Inhaler
Daedalian Member
|
Posted: Wed Jul 04, 2007 6:23 pm Post subject: 2 |
|
|
First of all, if the fullname is really supposed to be somebody's full name, then you have a buffer overflow whenever it's longer than 14 characters, which is not unlikely at all (my name is longer, and my name is not particularly long).
If that doesn't fix it, then I'd like to see the code that prints your array. |
|
| Back to top |
|
 |
cloud*
Guest
|
Posted: Wed Jul 04, 2007 7:15 pm Post subject: 3 |
|
|
| Quote: |
| First of all, if the fullname is really supposed to be somebody's full name, then you have a buffer overflow whenever it's longer than 14 characters, which is not unlikely at all (my name is longer, and my name is not particularly long). |
The names in the test file are pretty much one-word names. I'll increase the size later.
| Code: |
int i;
for(i=0;i<size;i++){
printf("%s %d %f\n", list[size].fullname, list[size].age,list[size].weight);
}
|
And the code's output:
| Code: |
0 0.000000
0 0.000000
0 0.000000
0 0.000000
0 0.000000
0 0.000000
0 0.000000
0 0.000000
0 0.000000
0 0.000000
|
As you can see, it prints out null for the person's name and zeros for the age and weight. I even tried hard-coding the first value in the array and outputting that, and it still didn't insert the values. |
|
| Back to top |
|
 |
Jack_Ian
Big Endian
|
Posted: Wed Jul 04, 2007 10:04 pm Post subject: 4 |
|
|
| cloud* wrote: |
| Code: |
int i;
for(i=0;i<size;i++){
printf("%s %d %f\n", list[size].fullname, list[size].age,list[size].weight);
}
|
|
try
| Code: |
printf("%s %d %f\n", list[i].fullname, list[i].age,list[i].weight); |
and get some sleep.  |
|
| Back to top |
|
 |
cloud*
Guest
|
Posted: Wed Jul 04, 2007 10:57 pm Post subject: 5 |
|
|
Sigh. I was so caught up in the structure of the array that I completely overlooked that.
Thanks a bunch, guys  |
|
| Back to top |
|
 |
Jack_Ian
Big Endian
|
Posted: Wed Jul 04, 2007 11:54 pm Post subject: 6 |
|
|
Happens to the best of us.
Glad I could help. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You can reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|