First create an instance of the structure then populate each PERSONAL structure, for example to populate the first PERSONAL structure
PERSON person;
strcpy(person.individual[0].name,"Tom");
Ancient Dragon
Retired & Loving It
30,046 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,342
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?
PERSON person;
then just pass person as a variable in a function?
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).
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
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
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
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
Pass the structure as a pointer to the function
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
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;
}
Ancient Dragon
Retired & Loving It
30,046 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,342
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.
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 ;
}
thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75