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).

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:

name
address
serial code
name
address
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:

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

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");

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).

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.

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

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. :)

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;
}
commented: Great help -Aia :) +1

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 ;
}

what does the function prototype extern double calc_area(float rad); indicate?

commented: Start your own thread, don't hijack someone else's -1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.