Question on nested structure

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2005
Posts: 20
Reputation: Savage221 is an unknown quantity at this point 
Solved Threads: 0
Savage221 Savage221 is offline Offline
Newbie Poster

Question on nested structure

 
0
  #1
Apr 8th, 2007
Hello, I had a question regarding nested structures. Lets say you *have* to use the following structures: (disregard the numbers thrown in, I just tossed random numbers in).

  1. typedef struct info{
  2. char name[20];
  3. char address[50];
  4. char serial[15];
  5. } PERSONAL;
  6.  
  7. typedef struct person{
  8. PERSONAL individual[50];
  9. int number;
  10. } PERSON;

I need to scan in all the data from a file. The file has the following format:
  1. name
  2. address
  3. serial code
  4. name
  5. address
  6. serial code
and repeats. Storing the data into a structure PERSONAL is easy, however, the instructions say I need to do it from PERSON. How do I go about that? What would be the declaration? Like, for PERSONAL, I could do the following:
  1. PERSONAL tom[MAX];
  2.  
  3. printf("%s", tom.address);

How would I do this if I had to store everything into PERSON from the get go? This is all in regular C
Last edited by Savage221; Apr 8th, 2007 at 4:57 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,494
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Question on nested structure

 
0
  #2
Apr 8th, 2007
First create an instance of the structure then populate each PERSONAL structure, for example to populate the first PERSONAL structure
  1. PERSON person;
  2. strcpy(person.individual[0].name,"Tom");
Last edited by Ancient Dragon; Apr 8th, 2007 at 5:51 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 20
Reputation: Savage221 is an unknown quantity at this point 
Solved Threads: 0
Savage221 Savage221 is offline Offline
Newbie Poster

Re: Question on nested structure

 
0
  #3
Apr 8th, 2007
Works beautifully, thank you. One last question: How would I go about passing that into a function?
The example you used was person.individual[0].name, suppose I wanted to declare this in a function (in a seperate .c file). Would I still simply do this in main?
  1. PERSON person;

then just pass person as a variable in a function?
  1. void Testing(PERSON name);
  2. (the prototype)
  3.  
  4. Testing(person);
  5. (the function call)
  6.  
  7. void Testing(PERSON name)
  8. {
  9. scanf("%s", name.individual[0].name);
  10. }
  11. (quick example of the definition)

I'm doing something wrong, as when I attempt to print out the contents I get a bunch of random characters (passing it through functions that is).
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,039
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Question on nested structure

 
0
  #4
Apr 8th, 2007
Originally Posted by Savage221 View Post
Works beautifully, thank you. One last question: How would I go about passing that into a function?
The example you used was person.individual[0].name, suppose I wanted to declare this in a function (in a seperate .c file). Would I still simply do this in main?
  1. PERSON person;
then just pass person as a variable in a function?
  1. void Testing(PERSON name);
  2. (the prototype)
  3.  
  4. Testing(person);
  5. (the function call)
  6.  
  7. void Testing(PERSON name)
  8. {
  9. scanf("%s", name.individual[0].name);
  10. }
  11. (quick example of the definition)
I'm doing something wrong, as when I attempt to print out the contents I get a bunch of random characters (passing it through functions that is).
I believe you have a problem of scope.
The value that you are scanning in the function is getting into the
copy that you created in that function but never get into the struct that came from when you called in main.
In other words, the struct inside the function is not the same inside
main so when you try to display it in main. What do you think is there?. Random data and that's what it shows you.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 20
Reputation: Savage221 is an unknown quantity at this point 
Solved Threads: 0
Savage221 Savage221 is offline Offline
Newbie Poster

Re: Question on nested structure

 
0
  #5
Apr 8th, 2007
Originally Posted by Aia View Post
I believe you have a problem of scope.
The value that you are scanning in the function is getting into the
copy that you created in that function but never get into the struct that came from when you called in main.
In other words, the struct inside the function is not the same inside
main so when you try to display it in main. What do you think is there?. Random data and that's what it shows you.
damn, youre right. I was assuming it would take the variable used in the function and store that into the original from main. Hmmm...how to get around this? What I need to do works fine if I throw it all in main (just for testing reasons), once I break it into functions its all messed up. Ill be messing around for a while trying to figure this out of course, however, as always, I appreciate any tips of suggestions. Thanks everyone
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,039
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Question on nested structure

 
0
  #6
Apr 8th, 2007
Originally Posted by Savage221 View Post
Hmmm...how to get around this?
intead of creating a void function return the struct. Is not the ideal, but
it would work until you start using pointers.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,494
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Question on nested structure

 
0
  #7
Apr 8th, 2007
Pass the structure as a pointer to the function
  1. void Testing(PERSON* name)
  2. {
  3. scanf("%s", name->individual[0].name);
  4. }

and don't use scanf() for strings because, like gets(), it will cause buffer overflows if you type in more characters than the destination buffer can hold. Use fgets() instead
  1. void Testing(PERSON* name)
  2. {
  3. char* ptr = 0;
  4. fgets( name->individual[0].name, sizeof( name->individual[0].name), stdin);
  5. // now remove the '\n' if it exists in the input buffer
  6. if( (ptr = strchr(name->individual[0].name,'\n')) != NULL)
  7. *ptr = 0;
  8. }
Last edited by Ancient Dragon; Apr 8th, 2007 at 10:32 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Question on nested structure

 
0
  #8
Apr 9th, 2007
Think your problem is solved.
Just wanted to comment on 2 things,
1. Be careful abt the length when you use scanf(), or follow what AD says.
2. the design part. If you have a function that is meant to take the name as input it would be unecessarry to pass it the whole structure (and thus make it dependent on the struct). You could just pass it the pointer to char* where he should store the name. Like this:
void getString( char* name ) ;

Advantage is clear of course.
1. your test function (getString in my code) doesn't need to depend on Person and it doesn't.
2. You can reuse the same function to get serial, address as well (anything that has the same format).
E.g.

  1. typedef struct info{
  2. char name[20];
  3. char address[50];
  4. char serial[15];
  5. } PERSONAL;
  6.  
  7. typedef struct person{
  8. PERSONAL individual[50];
  9. int number;
  10. } PERSON;
  11.  
  12.  
  13. void getString( char* str, int len )
  14. {
  15. char* ptr = 0;
  16. fgets( str, len, stdin);
  17. // now remove the '\n' if it exists in the input buffer
  18. if( (ptr = strchr(str,'\n')) != NULL)
  19. *ptr = 0;
  20. }
  21.  
  22. int main()
  23. {
  24. PERSON person ;
  25. printf("\"%s\"\n", person.individual[0].name ) ;
  26. getString( &(person.individual[0].name[0]), sizeof(person.individual[0].name) ) ;
  27. printf("\"%s\"\n", person.individual[0].name ) ;
  28.  
  29. getString( &(person.individual[0].serial[0]), sizeof(person.individual[0].serial) ) ;
  30. getString( &(person.individual[0].address[0]), sizeof(person.individual[0].address) ) ;
  31. return 0 ;
  32. }
Last edited by thekashyap; Apr 9th, 2007 at 2:30 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC