鱼C论坛

 找回密码
 立即注册
查看: 1436|回复: 5

[已解决]关于结构体数组初始化的一些问题

[复制链接]
发表于 2021-11-29 23:54:57 | 显示全部楼层 |阅读模式

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

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

x
今天在学习数据结构的过程中,发现了一个问题
如果我定义了一个结构体,在main函数中再声明一个结构体指针变量p
将p直接在子函数中给各元素赋值,编译器警告我的指针p没有初始化
如果我让struct Book *p = NULL,再次运行程序,编译器没有警告了,但是程序没有任何输出
到最后如果我使用malloc函数进行内存分配,程序运行正常。

由上所述,我遇到的问题就是在主函数中声明的结构体指针,如果没有进行内存分配,程序就无法正常运行,哪怕给结构体指针赋值NULL也不行。
但是,在我之前学习结构体时写的另一个程序中,我也是在主函数中声明了一个结构体指针,只是给他赋值了NULL,程序却正常运行了。

综上,上述两者很矛盾,一个结构体指针只需要赋值null程序就能够正常运行,另一个结构体指针却必须分配内存空间才能正常运行。
我有问题的代码如下:
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct Book{
  4.     int arrays[10];
  5.     int lens;
  6. };
  7. void InitLists(struct Book *t);

  8. void InitLists(struct Book *t){
  9.     for (int i = 0; i < 10;i++){
  10.     t->arrays[i] = 0;
  11.     }
  12.    
  13.     t->lens = 0;
  14. }

  15. int main(){
  16.     struct Book *p = NULL;  //如果只是赋值,程序无法运行
  17.     // p = (struct Book *)malloc(sizeof(struct Book));  //将这一行取消注释以后,程序正常运行
  18.     InitLists(p);
  19.     for (int i = 0; i < 10;i++){
  20.         printf("%d  =  %d\n", i, p->arrays[i]);
  21.     }
  22.     return 0;
  23. }
复制代码



下面的代码是我以前写的一个程序,代码很长,只需要关注main函数的前几行,我没有进行内存分配,程序仍然正常运行
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>

  4. struct Lists
  5. {
  6.     char name[128];
  7.     char num[20];
  8.     struct Lists *next;
  9. };
  10. void addPerson(struct Lists **head);
  11. void findPerson();
  12. void changePerson();
  13. void delPerson();
  14. void displayContacts();
  15. void endPerson();
  16. int welcome();

  17. int welcome(){
  18.     int num;
  19.     printf("欢迎使用通讯录管理程序!\n");
  20.     printf("1.插入新的联系人\n2.查找已有的联系人\n3.更改已有的联系人\n\
  21. 4.删除已有的联系人\n5.显示当前通讯录\n6.退出通讯录程序\n");
  22.     printf("请输入数字选择需要的服务:");
  23.     scanf("%d", &num);
  24.     getchar();
  25.     return num;
  26. }

  27. void addPerson(struct Lists **head){
  28.     struct Lists *temp;
  29.     temp = (struct Lists *)malloc(sizeof(struct Lists));
  30.     static struct Lists *prior=NULL;
  31.     // struct Lists *current=NULL;
  32.     static struct Lists *tail = NULL;
  33.     printf("您已选择插入新的联系人!\n");
  34.     printf("请输入联系人姓名:");
  35.     scanf("%s", temp->name);
  36.     printf("请输入联系人电话:");
  37.     scanf("%s", temp->num);
  38.     if(prior == NULL){
  39.         tail = temp;
  40.         prior = tail;
  41.         temp->next = NULL;
  42.         *head = temp;
  43.     }
  44.     else{
  45.         prior = tail;
  46.         tail->next = temp;
  47.         temp->next = NULL;
  48.         // (*head)->next =
  49.     }
  50.     printf("添加联系人成功!\n");
  51. }

  52. void findPerson(struct Lists **head){
  53.     struct Lists *temp;
  54.     temp = (struct Lists *)malloc(sizeof(struct Lists));
  55.     temp = *head;
  56.     printf("您已选择查找已有联系人!\n");
  57.     int ch;
  58.     printf("您是想根据姓名(1)查找还是根据号码(2)查找:");
  59.     scanf("%d", &ch);
  60.     switch (ch)
  61.     {
  62.     case 1:
  63.         printf("您已选择根据姓名查找!请输入姓名:");
  64.         char name[128];
  65.         scanf("%s", name);
  66.         while(temp != NULL && strcmp(temp->name,name)){
  67.             
  68.             temp = temp->next;
  69.         }
  70.         if(temp == NULL){
  71.             printf("没有找到符合的条件!\n");
  72.         }
  73.         else{
  74.             printf("找到适合的条件!\n");
  75.             printf("姓名:%s", temp->name);
  76.             printf("号码:%s", temp->num);
  77.         }
  78.         break;
  79.     case 2:
  80.         printf("您已选择根据号码查找!请输入号码:");
  81.         char num[20];
  82.         scanf("%s", num);
  83.         while(temp != NULL && strcmp(temp->num,num)){
  84.             temp = temp->next;
  85.         }
  86.         if(temp == NULL){
  87.             printf("没有找到符合的条件!\n");
  88.         }
  89.         else{
  90.             printf("找到适合的条件!\n");
  91.             printf("姓名:%s", temp->name);
  92.             printf("号码:%s", temp->num);
  93.         }
  94.         break;

  95.     default:
  96.         printf("请输入正确的选项!");
  97.         break;
  98.     }
  99. }

  100. void changePerson(struct Lists **head){
  101.     struct Lists *temp;
  102.     temp = (struct Lists *)malloc(sizeof(struct Lists));
  103.     temp = *head;
  104.     printf("您选择了更改已有联系人!");
  105.     printf("请输入您要更改的联系人姓名:");
  106.     char name[128];
  107.     scanf("%s", name);
  108.     while(temp != NULL && strcmp(temp->name,name)){
  109.         temp = temp->next;
  110.     }
  111.     if(temp == NULL){
  112.         printf("没有找到符合更改的名字!\n");
  113.     }
  114.     else{
  115.         printf("找到可以更改的联系人!\n");
  116.         printf("姓名:%s\n", temp->name);
  117.         printf("号码:%s\n", temp->num);
  118.         printf("您要更改姓名[1]还是号码[2]?");
  119.         int ch;
  120.         scanf("%d", &ch);
  121.         switch (ch)
  122.         {
  123.         case 1:
  124.             printf("请输入更改后的姓名:");
  125.             scanf("%s", temp->name);
  126.             break;
  127.         case 2:
  128.             printf("请输入更改后的号码:");
  129.             scanf("%s", temp->num);
  130.             break;

  131.         default:
  132.             printf("请输入正确的数字!");
  133.             break;
  134.         }
  135.     }
  136. }

  137. void delPerson(struct Lists **head){
  138.     printf("您选择了删除已有联系人\n");
  139.     printf("请输入您想要删除的联系人姓名:");
  140.     struct Lists *temp;
  141.     struct Lists *prior = NULL;

  142.     temp = (struct Lists *)malloc(sizeof(struct Lists));
  143.     temp = *head;
  144.     char name[128];
  145.     scanf("%s", name);
  146.     while(temp != NULL && strcmp(temp->name,name)){
  147.         prior = temp;
  148.         temp = temp->next;
  149.     }
  150.     if(temp == NULL){
  151.         printf("没有找到符合删除的名字!\n");
  152.     }
  153.     else{
  154.         printf("找到可以删除的联系人!\n");
  155.         printf("姓名:%s\n", temp->name);
  156.         printf("号码:%s\n", temp->num);
  157.         printf("确认删除吗?[1][0]");
  158.         int ch;
  159.         scanf("%d", &ch);
  160.         switch (ch)
  161.         {
  162.         case 1:
  163.             if(prior == NULL){
  164.                 *head = (*head)->next;
  165.             }
  166.             else{
  167.                 prior->next = temp->next;
  168.             };
  169.             // printf("@@@@@@@@@@@@@@@@%p\n", temp->next);
  170.             printf("已删除!");
  171.             break;
  172.         case 2:
  173.             printf("取消成功!");
  174.             break;

  175.         default:
  176.             printf("请输入正确的指令!");
  177.             break;
  178.         }
  179.     }
  180. }

  181. void displayContacts(struct Lists **head){
  182.     printf("以下为已存储的联系人信息:\n");
  183.     struct Lists *temp;
  184.     temp = (struct Lists *)malloc(sizeof(struct Lists));
  185.     temp = *head;
  186.     while(temp != NULL){
  187.         printf("========================================\n");
  188.         printf("姓名:%s\n", temp->name);
  189.         printf("号码:%s\n", temp->num);
  190.         temp = temp->next;
  191.     }
  192. }

  193. void endPerson(struct Lists **head){
  194.     printf("准备退出通讯录程序...\n");
  195.     _sleep(1000);
  196.     printf("开始释放内存...\n");
  197.     free(*head);
  198.     printf("内存释放完毕...\n再见!");
  199.     exit(1);
  200. }

  201. int main(void){
  202.     int choice;
  203.     struct Lists *head = NULL;
  204.     while(1){
  205.         choice = welcome();
  206.         switch (choice)
  207.         {
  208.         case 1:
  209.             addPerson(&head);
  210.             break;
  211.         case 2:
  212.             findPerson(&head);
  213.             break;
  214.         case 3:
  215.             changePerson(&head);
  216.             break;
  217.         case 4:
  218.             delPerson(&head);
  219.             break;
  220.         case 5:
  221.             displayContacts(&head);
  222.             break;
  223.         case 6:
  224.             endPerson(&head);
  225.             break;

  226.         default:
  227.             printf("输入错误!请重新输入!");
  228.         }

  229.     }
  230. }
复制代码
最佳答案
2021-11-30 06:24:04
本帖最后由 jhq999 于 2021-11-30 06:25 编辑
  1. temp = (struct Lists *)malloc(sizeof(struct Lists));//下面函数里
复制代码

上面的却没有申请内存空间,运行出错
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-11-30 06:24:04 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jhq999 于 2021-11-30 06:25 编辑
  1. temp = (struct Lists *)malloc(sizeof(struct Lists));//下面函数里
复制代码

上面的却没有申请内存空间,运行出错
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-11-30 22:25:44 | 显示全部楼层
我可不可以这样理解,只要声明了一个结构体变量,我就要给他分配内存空间?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-11-30 22:26:29 | 显示全部楼层
jhq999 发表于 2021-11-30 06:24
上面的却没有申请内存空间,运行出错

谢谢您的回复,
我可不可以这样理解,只要声明了一个结构体变量,我就要给他分配内存空间?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-12-1 06:05:21 | 显示全部楼层
GodLordGee 发表于 2021-11-30 22:26
谢谢您的回复,
我可不可以这样理解,只要声明了一个结构体变量,我就要给他分配内存空间?

结构体变量已经实例化,有自己的内存空间。
说的是结构体指针变量,用的时候可以申请新的内存空间,也可以把现有的结构体实例的指针赋值给它,
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-1 22:29:12 | 显示全部楼层
jhq999 发表于 2021-12-1 06:05
结构体变量已经实例化,有自己的内存空间。
说的是结构体指针变量,用的时候可以申请新的内存空间,也可 ...

谢谢您的回复!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-25 21:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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