Nested structure in c

I need to create a nested structure in order to store some basic information about a person (name, age, address). So I created a structure called "information", and to store the address, I created another nested structure inside "info" called "address". But whenever I suggest storing values ​​with a for loop, I get errors. What is the problem and how can I solve it?

[Error] 'struct Info' does not have a name named 'address'
Declaration [Warning] does not declare anything [enabled by default]

#include <stdio.h>

int main(){

    struct Info{
        char name[30];
        int age;
        struct address{
            char area_name[39];
            int house_no;
            char district[39];
        };
    };

    struct Info Person[10];

    int i;
    for(i=0;i<10;i++){

        printf("enter info of person no %d\n",i);
        printf("enter name\n");
        scanf("%s",&Person[i].name);
        printf("enter age\n");
        scanf("%d",&Person[i].age);
        printf("enter address :\n");
        printf("enter area name :\n");
        scanf("%s",&Person[i].address.area_name);
        printf("enter house no : \n");
        scanf("%d",&Person[i].address.house_no);
        printf("enter district : \n");
        scanf("%s",&Person[i].address.district);
    }
}
+4
source share
5 answers

struct address Info, .

, ,

struct Info{
    char name[30];
    int age;
    struct address{
        char area_name[39];
        int house_no;
        char district[39];
    } address;
      ^^^^^^^^
};
+7

Info address, - address.

struct Info
{
    ...
    struct
    {
        ...
    } address;
};
+4

, , - address, address struct Info Person[i].address.

address:

struct Info{
    char name[30];
    int age;
    struct {
        char area_name[39];
        int house_no;
        char district[39];
    } address; // <<< here it is now
};

- :

struct Info{
    char name[30];
    int age;
    struct addr{ // as noted by @JonathanLeffler,
                 // it not necessary to change the 
                 // name of a struct
        char area_name[39];
        int house_no;
        char district[39];
    };
    struct addr address; // <<< a variable of type struct addr
};
+4

http://www.c4learn.com/c-programming/c-nested-structure/ , "" , ..

struct address{
    char area_name[39];
    int house_no;
    char district[39];
} adr_;

, :

&Person[i].adr_.house_no
+1

Here is the updated code using an anonymous structure . I have included C11 for compilation.

#include <stdio.h>

int main(){

    struct Info{
        char name[30];
        int age;
        struct {
            char area_name[39];
            int house_no;
            char district[39];
        };
    };

    struct Info Person[10];

    int i;
    for(i=0;i<10;i++){

        printf("enter info of person no %d\n",i);
        printf("enter name\n");
        scanf(" %s",Person[i].name);
        printf("enter age\n");
        scanf("%d",&Person[i].age);
        printf("enter address :\n");
        printf("enter area name :\n");
        scanf(" %s",Person[i].area_name);
        printf("enter house no : \n");
        scanf("%d",&Person[i].house_no);
        printf("enter district : \n");
        scanf(" %s",Person[i].district);
    }
}
0
source

All Articles