944,198 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 7816
  • C RSS
Apr 8th, 2007
0

Question on nested structure

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Savage221 is offline Offline
20 posts
since Oct 2005
Apr 8th, 2007
0

Re: Question on nested structure

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Apr 8th, 2007
0

Re: Question on nested structure

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).
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Savage221 is offline Offline
20 posts
since Oct 2005
Apr 8th, 2007
0

Re: Question on nested structure

Click to Expand / Collapse  Quote originally posted by Savage221 ...
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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Apr 8th, 2007
0

Re: Question on nested structure

Click to Expand / Collapse  Quote originally posted by Aia ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Savage221 is offline Offline
20 posts
since Oct 2005
Apr 8th, 2007
0

Re: Question on nested structure

Click to Expand / Collapse  Quote originally posted by Savage221 ...
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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Apr 8th, 2007
0

Re: Question on nested structure

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Apr 9th, 2007
0

Re: Question on nested structure

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.
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Jun 4th, 2010
-2
Re: Question on nested structure
what does the function prototype extern double calc_area(float rad); indicate?
Reputation Points: 9
Solved Threads: 0
Newbie Poster
karthikeyans is offline Offline
1 posts
since Jun 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Please! i need it to work for big integers ,,
Next Thread in C Forum Timeline: Reading and printing





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC