| | |
Question on nested structure
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2005
Posts: 20
Reputation:
Solved Threads: 0
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).
I need to scan in all the data from a file. The file has the following format:
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:
How would I do this if I had to store everything into PERSON from the get go? This is all in regular C
C Syntax (Toggle Plain Text)
typedef struct info{ char name[20]; char address[50]; char serial[15]; } PERSONAL; typedef struct person{ PERSONAL individual[50]; int number; } PERSON;
I need to scan in all the data from a file. The file has the following format:
C Syntax (Toggle Plain Text)
name address serial code name address serial code
C Syntax (Toggle Plain Text)
PERSONAL tom[MAX]; 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.
First create an instance of the structure then populate each PERSONAL structure, for example to populate the first PERSONAL structure
C Syntax (Toggle Plain Text)
PERSON person; 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.
•
•
Join Date: Oct 2005
Posts: 20
Reputation:
Solved Threads: 0
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?
then just pass person as a variable in a function?
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).
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?
C Syntax (Toggle Plain Text)
PERSON person;
then just pass person as a variable in a function?
C Syntax (Toggle Plain Text)
void Testing(PERSON name); (the prototype) Testing(person); (the function call) void Testing(PERSON name) { scanf("%s", name.individual[0].name); } (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).
•
•
•
•
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?
then just pass person as a variable in a function?C Syntax (Toggle Plain Text)
PERSON person;
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).C Syntax (Toggle Plain Text)
void Testing(PERSON name); (the prototype) Testing(person); (the function call) void Testing(PERSON name) { scanf("%s", name.individual[0].name); } (quick example of the definition)
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.
•
•
Join Date: Oct 2005
Posts: 20
Reputation:
Solved Threads: 0
•
•
•
•
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.
Pass the structure as a pointer to the function
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
C Syntax (Toggle Plain Text)
void Testing(PERSON* name) { scanf("%s", name->individual[0].name); }
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
C Syntax (Toggle Plain Text)
void Testing(PERSON* name) { char* ptr = 0; fgets( name->individual[0].name, sizeof( name->individual[0].name), stdin); // now remove the '\n' if it exists in the input buffer if( (ptr = strchr(name->individual[0].name,'\n')) != NULL) *ptr = 0; }
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.
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:
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.
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.
c Syntax (Toggle Plain Text)
typedef struct info{ char name[20]; char address[50]; char serial[15]; } PERSONAL; typedef struct person{ PERSONAL individual[50]; int number; } PERSON; void getString( char* str, int len ) { char* ptr = 0; fgets( str, len, stdin); // now remove the '\n' if it exists in the input buffer if( (ptr = strchr(str,'\n')) != NULL) *ptr = 0; } int main() { PERSON person ; printf("\"%s\"\n", person.individual[0].name ) ; getString( &(person.individual[0].name[0]), sizeof(person.individual[0].name) ) ; printf("\"%s\"\n", person.individual[0].name ) ; getString( &(person.individual[0].serial[0]), sizeof(person.individual[0].serial) ) ; getString( &(person.individual[0].address[0]), sizeof(person.individual[0].address) ) ; return 0 ; }
Last edited by thekashyap; Apr 9th, 2007 at 2:30 am.
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: Newlines in txt files take 2 bytes?
- Next Thread: letter p
| Thread Tools | Search this Thread |
adobe ansi api array arrays asterisks bash binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql number open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer pointers posix power probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming stack standard strchr string strings structures suggestions systemcall test testautomation unix user variable voidmain() wab win32api windows.h






