c中的嵌套结构 [英] Nested structure in c

查看:70
本文介绍了c中的嵌套结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须构建一个嵌套结构来存储有关某人的一些基本信息(姓名,年龄,地址).因此,我创建了一个名为"info"的结构,并保存了地址,然后在"info"中创建了另一个嵌套结构,称为"address".但是,每当我提示使用for循环存储值时,都会出现错误.这是什么问题,我该如何解决?

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

推荐答案

您在结构 Info 中声明了类型 struct address ,但没有声明此类型的数据成员.

You declared a type struct address in the structure Info but not a data member of this type.

您可以编写示例

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

这篇关于c中的嵌套结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆