问题
I have to build a nested structure to store some basic information about some person (name, age, address). So I created a structure called "info" and to hold the address I created another nested structure inside "info" called "address". But whenever I prompt to store the values using a for loop, I get errors. What is the problem here and how can I solve it?
[Error] 'struct Info' has no member named 'address'
[Warning] declaration 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);
}
}
回答1:
You declared a type struct address
in the structure Info
but not a data member of this type.
You can write for example
struct Info{
char name[30];
int age;
struct address{
char area_name[39];
int house_no;
char district[39];
} address;
^^^^^^^^
};
回答2:
What you have at the moment is just a declaration of a structure called address
, but you'll need a variable called address
in struct Info
to use the Person[i].address
syntax.
What you need is to move the word address
a bit:
struct Info{
char name[30];
int age;
struct {
char area_name[39];
int house_no;
char district[39];
} address; // <<< here it is now
};
Another option is to use the following:
struct Info{
char name[30];
int age;
struct addr{ // as noted by @JonathanLeffler,
// it's 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
};
回答3:
The structure Info
have a nested structure named address
, but not a member variable named address
.
You should do
struct Info
{
...
struct
{
...
} address;
};
回答4:
From http://www.c4learn.com/c-programming/c-nested-structure/ it looks like you should "dub" your address structure, i.e.
struct address{
char area_name[39];
int house_no;
char district[39];
} adr_;
Then to set house number:
&Person[i].adr_.house_no
回答5:
Here is the updated code using anonymous structure. I enabled C11 for compiling this.
#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);
}
}
来源:https://stackoverflow.com/questions/35869873/nested-structure-in-c