The Grey Labyrinth is a collection of puzzles, riddles, mind games, paradoxes and other intellectually challenging diversions. Related topics: puzzle games, logic puzzles, lateral thinking puzzles, philosophy, mind benders, brain teasers, word problems, conundrums, 3d puzzles, spatial reasoning, intelligence tests, mathematical diversions, paradoxes, physics problems, reasoning, math, science.

   
The Grey Labyrinth Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups    RegisterRegister  
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Any C wizards out there?

 
Reply to topic    The Grey Labyrinth Forum Index -> Science, Art, and Culture
View previous topic :: View next topic  
Author Message
cloudRunner
Daedalian Member



PostPosted: Wed Jul 04, 2007 5:16 pm    Post subject: 1 Reply with quote

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
View user's profile Send private message
Bicho the Inhaler
Daedalian Member



PostPosted: Wed Jul 04, 2007 6:23 pm    Post subject: 2 Reply with 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).

If that doesn't fix it, then I'd like to see the code that prints your array.
Back to top
View user's profile Send private message
cloud*
Guest



PostPosted: Wed Jul 04, 2007 7:15 pm    Post subject: 3 Reply with quote

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



PostPosted: Wed Jul 04, 2007 10:04 pm    Post subject: 4 Reply with quote

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. Wink
Back to top
View user's profile Send private message
cloud*
Guest



PostPosted: Wed Jul 04, 2007 10:57 pm    Post subject: 5 Reply with quote

Surprised

Sigh. I was so caught up in the structure of the array that I completely overlooked that. Mad

Thanks a bunch, guys Felicitous
Back to top
Jack_Ian
Big Endian



PostPosted: Wed Jul 04, 2007 11:54 pm    Post subject: 6 Reply with quote

Happens to the best of us.
Glad I could help.
Back to top
View user's profile Send private message
Display posts from previous: by   
Reply to topic    The Grey Labyrinth Forum Index -> Science, Art, and Culture All times are GMT
Page 1 of 1

 
Jump to:  
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
Site Design by Wx3