鱼C论坛

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

动态链表的建立于输出问题

[复制链接]
发表于 2014-5-20 23:43:26 | 显示全部楼层 |阅读模式

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

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

x
  1. //建立一个链表存放学生的信息

  2. struct student
  3.         {
  4.                 char *name;
  5.                 char sex;
  6.                 int age;
  7.                 struct student * next;
  8.         };


  9. //建立动态链表
  10. struct student *create()
  11. {
  12.         struct student *head,*p1,*p2;
  13.         int n = 0;
  14.         p1 = p2 = (struct student*)malloc(sizeof(struct student));
  15.         ZeroMemory(p1,sizeof(struct student));
  16.         head = NULL;
  17.         scanf("%s",p1->name);
  18.         scanf("%c",&p1->sex);
  19.         scanf("%d",&p1->age);

  20.         while(p1->name != NULL)
  21.         {
  22.                 n = n +1;
  23.                 if(n == 1)head = p1;
  24.                 else p2->next = p1;
  25.                 p2 = p1;
  26.                 p1 =(struct student*)malloc(sizeof(struct student));
  27.                 scanf("%s",p1->name);
  28.             scanf("%c",&p1->sex);
  29.                 scanf("%d",&p1->age);
  30.         }
  31.         p2->next = NULL;
  32.         return head;
  33. }


  34. int main(int argc, char* argv[])
  35. {
  36.         struct student *pt;
  37.         printf("请输入姓名、性别(M\F)、年龄:\n");
  38.         pt = create();
  39.         while(pt != NULL)
  40.         {
  41.                 printf("%s\t%c\t%d",pt->name,pt->sex,pt->age);
  42.                 pt = pt->next;
  43.         }
  44.         system("pause");
  45.         return 0;
  46. }
复制代码
请问大神这是怎么回事?
QQ浏览器截屏未命名.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-5-21 01:06:29 | 显示全部楼层
结构体里姓名定义是一个没用的指针!!野指针……
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-5-21 01:51:02 | 显示全部楼层
char *name = NULL;
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-5-21 09:26:37 | 显示全部楼层
:lol:
灰常感谢大神救助:lol:
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-5-21 13:12:59 | 显示全部楼层
  1. // test.cpp : Defines the entry point for the console application.
  2. //

  3. #include "stdafx.h"
  4. #include <stdlib.h>
  5. #include <windows.h>


  6. //建立一个链表存放学生的信息

  7. struct student
  8.         {
  9.                 char name[20];
  10.                 char sex;
  11.                 int age;
  12.                 struct student * next;
  13.         };


  14. //建立动态链表
  15. struct student *create()
  16. {
  17.         struct student *head,*p1,*p2;
  18.         int n = 0;
  19.         p1 = p2 = (struct student*)malloc(sizeof(struct student));
  20.         ZeroMemory(p1,sizeof(struct student));
  21.         head = NULL;
  22.         scanf("%s %c %d",p1->name,&p1->sex,&p1->age);

  23.         while(strcmp(p1->name,"end") != 0)
  24.         {
  25.                 n = n +1;
  26.                 if(n == 1)
  27.                 {
  28.                         head = p1;
  29.                         printf("access done!\n");
  30.                 }
  31.                 else p2->next = p1;
  32.                 p2 = p1;
  33.                 p1 =(struct student*)malloc(sizeof(struct student));
  34.                 ZeroMemory(p1,sizeof(struct student));
  35.                 scanf("%s %c %d",p1->name,&p1->sex,&p1->age);
  36.                 printf("access done!\n");
  37.         }
  38.         p2->next = NULL;
  39.         return head;
  40. }


  41. int main(int argc, char* argv[])
  42. {
  43.         struct student *pt;
  44.         printf("请输入姓名、性别(M\F)、年龄:\n");
  45.         pt = create();
  46.         if(pt != NULL)
  47.                 printf("姓名\t性别\t年龄\n");
  48.         while(pt != NULL)
  49.         {
  50.                 printf("%s\t%c\t%d\n",pt->name,pt->sex,pt->age);
  51.                 pt = pt->next;
  52.         }
  53.         system("pause");
  54.         return 0;
  55. }
复制代码
附上完整代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-5-21 13:14:55 | 显示全部楼层
在对指针进行操作的时候,不管指针在结构体内还是在别处,都要先分配内存才可以使用
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-22 06:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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