鱼C论坛

 找回密码
 立即注册
查看: 1339|回复: 13

[已解决]结构体

[复制链接]
发表于 2021-3-6 14:58:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<stdio.h>
#include<string.h>
#define add struct addresslist

        void inquire(char name1[10])
        {
                int b=0;
                for(b;b<50;b++)
                {
/*9*/                        if(strcmp(person[b].name,name1)==0)
/*10*/                                printf("年龄:%d\n电话:%d",person[b].year,person[b].phone);
                        if(b==50)
                        {
                                printf("查无此人");
                        }
                }
        }
        struct addresslist
        {
                char name[10];
                int year;
                int phone;
        }person[50]={"小红",19,15826211111,"小明",18,13666666666},*p;
int main()
{
        int a,b,c=2,d,e=1;
        char name1[10];
        printf("\t手机通讯录\n");
        printf("是否查询通讯人(查询请按1,不查询请按2)");
        scanf("%d",&a);
        if(a==1)
        {
                printf("请输入通讯人名字:");
                gets(name1);
                inquire(name1);
        }
        printf("是否新建联系人?(是请按1,不是请按2)");
        scanf("%d",&b);
        if(b==1)
        {
                for(c;c<50;c++)
                {
                        printf("请输入名字:");
                        scanf("%s",person[c].name);
                        printf("\n请输入年龄:");
                        scanf("%d",person[c].year);
                        printf("\n请输入电话:");
                        scanf("%d",person[c].phone);
                        printf("\n已经建立一个新的联系人(是否还要再建立联系人?(要请输入1,不要请输入2))\n");
                        scanf("%d",&d);
                        if(d==2)
                        {
                                e++;
                                break;
                        }
                }
                if(e>1)
                {
                        printf("你已经成功新建联系人,现在手机新添联系人有:\n");
                                for(e;(e<50)&&(person[e].name);e++)
                                        printf("姓名;%s\n",person[e].name);
                                printf("年龄:%d\n",person[e].year);
                                printf("电话:%d",person[e].phone);
                }
        }
        return 0;
}


这是设计一个手机通讯录,最多容纳50人的基本信息,姓名,电话,年龄,通讯录还具有新建和查询功能。我输入的/*9*/行和下面一行都报错说person是未声明标识符。我前面不是有一个person[50]吗?
最佳答案
2021-3-6 15:31:40
编译器说你错了,那肯定就是你错了,没有商量的余地,这时候最好的做法就是认真检查你的代码

  1. #include<stdio.h>
  2. #include<string.h>
  3. #define add struct addresslist

  4. struct addresslist
  5. {
  6.     char name[10];
  7.     int year;
  8.     int phone;
  9. }person[50]={"小红",19,15826211111,"小明",18,13666666666},*p;

  10. void inquire(char name1[10])
  11. {
  12.     int b=0;
  13.     for(b;b<50;b++)
  14.     {
  15.         /*9*/                        if(strcmp(person[b].name,name1)==0)
  16.             /*10*/                                printf("年龄:%d\n电话:%d",person[b].year,person[b].phone);
  17.         if(b==50)
  18.         {
  19.             printf("查无此人");
  20.         }
  21.     }
  22. }

  23. int main()
  24. {
  25.     int a,b,c=2,d,e=1;
  26.     char name1[10];
  27.     printf("\t手机通讯录\n");
  28.     printf("是否查询通讯人(查询请按1,不查询请按2)");
  29.     scanf("%d",&a);
  30.     if(a==1)
  31.     {
  32.         printf("请输入通讯人名字:");
  33.         gets(name1);
  34.         inquire(name1);
  35.     }
  36.     printf("是否新建联系人?(是请按1,不是请按2)");
  37.     scanf("%d",&b);
  38.     if(b==1)
  39.     {
  40.         for(c;c<50;c++)
  41.         {
  42.             printf("请输入名字:");
  43.             scanf("%s",person[c].name);
  44.             printf("\n请输入年龄:");
  45.             scanf("%d",person[c].year);
  46.             printf("\n请输入电话:");
  47.             scanf("%d",person[c].phone);
  48.             printf("\n已经建立一个新的联系人(是否还要再建立联系人?(要请输入1,不要请输入2))\n");
  49.             scanf("%d",&d);
  50.             if(d==2)
  51.             {
  52.                 e++;
  53.                 break;
  54.             }
  55.         }
  56.         if(e>1)
  57.         {
  58.             printf("你已经成功新建联系人,现在手机新添联系人有:\n");
  59.             for(e;(e<50)&&(person[e].name);e++)
  60.                 printf("姓名;%s\n",person[e].name);
  61.             printf("年龄:%d\n",person[e].year);
  62.             printf("电话:%d",person[e].phone);
  63.         }
  64.     }
  65.     return 0;
  66. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-3-6 15:31:40 | 显示全部楼层    本楼为最佳答案   
编译器说你错了,那肯定就是你错了,没有商量的余地,这时候最好的做法就是认真检查你的代码

  1. #include<stdio.h>
  2. #include<string.h>
  3. #define add struct addresslist

  4. struct addresslist
  5. {
  6.     char name[10];
  7.     int year;
  8.     int phone;
  9. }person[50]={"小红",19,15826211111,"小明",18,13666666666},*p;

  10. void inquire(char name1[10])
  11. {
  12.     int b=0;
  13.     for(b;b<50;b++)
  14.     {
  15.         /*9*/                        if(strcmp(person[b].name,name1)==0)
  16.             /*10*/                                printf("年龄:%d\n电话:%d",person[b].year,person[b].phone);
  17.         if(b==50)
  18.         {
  19.             printf("查无此人");
  20.         }
  21.     }
  22. }

  23. int main()
  24. {
  25.     int a,b,c=2,d,e=1;
  26.     char name1[10];
  27.     printf("\t手机通讯录\n");
  28.     printf("是否查询通讯人(查询请按1,不查询请按2)");
  29.     scanf("%d",&a);
  30.     if(a==1)
  31.     {
  32.         printf("请输入通讯人名字:");
  33.         gets(name1);
  34.         inquire(name1);
  35.     }
  36.     printf("是否新建联系人?(是请按1,不是请按2)");
  37.     scanf("%d",&b);
  38.     if(b==1)
  39.     {
  40.         for(c;c<50;c++)
  41.         {
  42.             printf("请输入名字:");
  43.             scanf("%s",person[c].name);
  44.             printf("\n请输入年龄:");
  45.             scanf("%d",person[c].year);
  46.             printf("\n请输入电话:");
  47.             scanf("%d",person[c].phone);
  48.             printf("\n已经建立一个新的联系人(是否还要再建立联系人?(要请输入1,不要请输入2))\n");
  49.             scanf("%d",&d);
  50.             if(d==2)
  51.             {
  52.                 e++;
  53.                 break;
  54.             }
  55.         }
  56.         if(e>1)
  57.         {
  58.             printf("你已经成功新建联系人,现在手机新添联系人有:\n");
  59.             for(e;(e<50)&&(person[e].name);e++)
  60.                 printf("姓名;%s\n",person[e].name);
  61.             printf("年龄:%d\n",person[e].year);
  62.             printf("电话:%d",person[e].phone);
  63.         }
  64.     }
  65.     return 0;
  66. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-6 15:34:28 | 显示全部楼层
另外,编译器还说你有这么多的问题
  1. $ gcc -O3 -g -Wall -o main main.c
  2. main.c:10:26: warning: overflow in conversion from ‘long int’ to ‘int’ changes value from ‘15826211111’ to ‘-1353658073’ [-Woverflow]
  3.    10 | }person[50]={"小红",19,15826211111,"小明",18,13666666666},*p;
  4.       |                        ^~~~~~~~~~~
  5. main.c:10:50: warning: overflow in conversion from ‘long int’ to ‘int’ changes value from ‘13666666666’ to ‘781764778’ [-Woverflow]
  6.    10 | }person[50]={"小红",19,15826211111,"小明",18,13666666666},*p;
  7.       |                                              ^~~~~~~~~~~
  8. main.c:10:13: warning: missing braces around initializer [-Wmissing-braces]
  9.    10 | }person[50]={"小红",19,15826211111,"小明",18,13666666666},*p;
  10.       |             ^
  11.       |              {                    }{                    }
  12. main.c: In function ‘inquire’:
  13. main.c:15:5: warning: statement with no effect [-Wunused-value]
  14.    15 |     for(b;b<50;b++)
  15.       |     ^~~
  16. main.c: In function ‘main’:
  17. main.c:43:9: warning: statement with no effect [-Wunused-value]
  18.    43 |         for(c;c<50;c++)
  19.       |         ^~~
  20. main.c:48:21: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
  21.    48 |             scanf("%d",person[c].year);
  22.       |                    ~^  ~~~~~~~~~~~~~~
  23.       |                     |           |
  24.       |                     int *       int
  25. main.c:50:21: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
  26.    50 |             scanf("%d",person[c].phone);
  27.       |                    ~^  ~~~~~~~~~~~~~~~
  28.       |                     |           |
  29.       |                     int *       int
  30. main.c:62:13: warning: statement with no effect [-Wunused-value]
  31.    62 |             for(e;(e<50)&&(person[e].name);e++)
  32.       |             ^~~
  33. main.c:64:40: warning: array subscript 50 is above array bounds of ‘struct addresslist[50]’ [-Warray-bounds]
  34.    64 |             printf("年龄:%d\n",person[e].year);
  35.       |                                ~~~~~~^~~
  36. main.c:10:2: note: while referencing ‘person’
  37.    10 | }person[50]={"小红",19,15826211111,"小明",18,13666666666},*p;
  38.       |  ^~~~~~
  39. main.c:65:38: warning: array subscript 50 is above array bounds of ‘struct addresslist[50]’ [-Warray-bounds]
  40.    65 |             printf("电话:%d",person[e].phone);
  41.       |                              ~~~~~~^~~
  42. main.c:10:2: note: while referencing ‘person’
  43.    10 | }person[50]={"小红",19,15826211111,"小明",18,13666666666},*p;
  44.       |  ^~~~~~

复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-6 19:35:23 | 显示全部楼层
人造人 发表于 2021-3-6 15:34
另外,编译器还说你有这么多的问题

warning到不用管,我是这么看到
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-6 19:43:57 | 显示全部楼层
严凯 发表于 2021-3-6 19:35
warning到不用管,我是这么看到

warning怎么能不管呢?编译器说你有可能出问题了,那么你就真的很有可能出问题了

  1. main.c:50:21: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
  2.    50 |             scanf("%d",person[c].phone);
复制代码


这对吗 ?
  1. scanf("%d",person[c].phone);
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-6 19:50:36 | 显示全部楼层
本帖最后由 人造人 于 2021-3-6 19:57 编辑

编译器说%d要一个int *, 你给的是一个 int
难道不应该是这样吗?

  1. scanf("%d", &person[c].phone);
复制代码



总结:不要忽略编译器对你说的任何话
编译器对你说了什么,你应该感到高兴,因为编译器帮你检查出问题了,如果编译器一声不吭,可不见得是件好事
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-6 20:34:59 | 显示全部楼层
本帖最后由 墙里秋千墙外荡 于 2021-3-6 21:06 编辑
  1. struct addresslist
  2.         {
  3.                 char name[10];
  4.                 int year;
  5.                 int phone;
  6.         }person[50]={"小红",19,15826211111,"小明",18,13666666666},*p;
复制代码

把这段代码放到函数inquire(char name1[10])前面

编译器是按顺序编译的,如果定义在后,使用在前就会出现这种情况,不是说你定义了就行。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-6 21:11:13 | 显示全部楼层
人造人 发表于 2021-3-6 15:34
另外,编译器还说你有这么多的问题

出现错误,先修改第一个错误,其他错误可能都是第一个错误导致的,修改了第一个,后面的部分错误可能也解决了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-6 21:19:58 | 显示全部楼层
墙里秋千墙外荡 发表于 2021-3-6 21:11
出现错误,先修改第一个错误,其他错误可能都是第一个错误导致的,修改了第一个,后面的部分错误可能也解 ...

嗯,我同意你的观点,^_^
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-7 10:30:51 | 显示全部楼层
人造人 发表于 2021-3-6 21:19
嗯,我同意你的观点,^_^

额,哪个我试了一下,现在就是查询的时候,我按了1之后,就表示我要查询,我后面有一个gets(name1),没有起到作用,按了1之后,就输出很多年龄0,电话0,这是一个问题,还有一个就是,我先不查询,先新建联系人,当我已经输入姓名,年龄之后,要输入电话的时候,程序就自动结束了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-7 10:31:53 | 显示全部楼层
人造人 发表于 2021-3-6 21:19
嗯,我同意你的观点,^_^

看不懂电脑的警告,就知道大概它在警告哪里
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-7 10:51:39 | 显示全部楼层
严凯 发表于 2021-3-7 10:30
额,哪个我试了一下,现在就是查询的时候,我按了1之后,就表示我要查询,我后面有一个gets(name1),没有 ...

把你现在的代码贴出来
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-7 12:56:21 | 显示全部楼层
人造人 发表于 2021-3-7 10:51
把你现在的代码贴出来

#include<stdio.h>
#include<string.h>
#define add struct addresslist

struct addresslist
{
    char name[10];
    int year;
    int phone;
}person[50]={"小红",19,15826211111,"小明",18,13666666666},*p;

void inquire(char name1[10])
{
    int b=0;
    for(b;b<50;b++)
    {
                            if(strcmp(person[b].name,name1)==0)
                                            printf("年龄:%d\n电话:%d",person[b].year,person[b].phone);
        if(b==50)
        {
            printf("查无此人");
        }
    }
}

int main()
{
    int a,b,c=2,d,e=1;
    char name1[10];
    printf("\t手机通讯录\n");
    printf("是否查询通讯人(查询请按1,不查询请按2)");
    scanf("%d",&a);
    if(a==1)
    {
        printf("请输入通讯人名字:");
        gets(name1);
        inquire(name1);
    }
    printf("是否新建联系人?(是请按1,不是请按2)");
    scanf("%d",&b);
    if(b==1)
    {
        for(c;c<50;c++)
        {
            printf("请输入名字:");
            scanf("%s",person[c].name);
            printf("\n请输入年龄:");
            scanf("%d",person[c].year);
            printf("\n请输入电话:");
            scanf("%d",person[c].phone);
            printf("\n已经建立一个新的联系人(是否还要再建立联系人?(要请输入1,不要请输入2))\n");
            scanf("%d",&d);
            if(d==2)
            {
                e++;
                break;
            }
        }
        if(e>1)
        {
            printf("你已经成功新建联系人,现在手机新添联系人有:\n");
            for(e;(e<50)&&(person[e].name);e++)
                printf("姓名;%s\n",person[e].name);
            printf("年龄:%d\n",person[e].year);
            printf("电话:%d",person[e].phone);
        }
    }
    return 0;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-7 13:01:05 | 显示全部楼层
严凯 发表于 2021-3-7 12:56
#include
#include
#define add struct addresslist

你就没有修改编译器指出的问题吧,这怎么可能正确?
  1. scanf("%d",person[c].phone);
复制代码


scanf函数是这样用的吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-12 18:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表