user-0 提问: 初学动态链表一堆错误不知道是什么情况

回复:
user: user-0
content: if (!p) 		exit(0); 	p->name = (char*)malloc(sizeof(char) * len); 	if (!p->name) 		exit(0); 这个是什么情况没怎么见过

user: user-0
content: 引用 4 楼 自信男孩 的回复:#include<stdio.h> #include<stdlib.h> #include<string.h> #define len 20 struct student {     char* name;     struct student* next; }; struct student* ctrn() {     char max[len],a;     struct student * p, * hard, * next;      //p = (struct student*)malloc(sizeof(char));     p = (struct student*)malloc(sizeof(struct student));     if (!p)         exit(0);     p->name = (char *)malloc(sizeof(char) * len);     if (!p->name)         exit(0);     gets_s(max, len);     strcpy(p->name, max);      printf("是否继续输入(y/n):");     scanf_s("%c", &a);     //while (a = 'y')     while (a == 'y')     {         //next= (struct student*)malloc(sizeof(char));         next= (struct student*)malloc(sizeof(struct student));         if (!next)             exit(0);         next->name = (char *)malloc(sizeof(char) * len);         if (!next->name)             exit(0);         gets_s(max, len);         strcpy(next->name, max);         next= next->next;         if (a == 'n')         {             next->next= NULL;             break;         }     }     p->next = next;     hard = p;     while (1)     {         //printf("%d", p->name);         printf("%s", p->name);         p = p->next;         if (p->next != NULL)         {             break;         }     } }  供参考～ 大佬又是你啊，谢谢你了

user: user-1
content: #include<stdio.h> #include<stdlib.h> #include<string.h> #define len 20 struct student {     char* name;     struct student* next; }; struct student* ctrn() {     char max[len],a;     struct student * p, * hard, * next;      //p = (struct student*)malloc(sizeof(char));     p = (struct student*)malloc(sizeof(struct student));     if (!p)         exit(0);     p->name = (char *)malloc(sizeof(char) * len);     if (!p->name)         exit(0);     gets_s(max, len);     strcpy(p->name, max);      printf("是否继续输入(y/n):");     scanf_s("%c", &a);     //while (a = 'y')     while (a == 'y')     {         //next= (struct student*)malloc(sizeof(char));         next= (struct student*)malloc(sizeof(struct student));         if (!next)             exit(0);         next->name = (char *)malloc(sizeof(char) * len);         if (!next->name)             exit(0);         gets_s(max, len);         strcpy(next->name, max);         next= next->next;         if (a == 'n')         {             next->next= NULL;             break;         }     }     p->next = next;     hard = p;     while (1)     {         //printf("%d", p->name);         printf("%s", p->name);         p = p->next;         if (p->next != NULL)         {             break;         }     } }  供参考～

user: user-0
content: 引用 8 楼 自信男孩 的回复:[quote=引用 7 楼 Gnglas 的回复:] [quote=引用 4 楼 自信男孩的回复:]#include<stdio.h> #include<stdlib.h> #include<string.h> #define len 20 struct student {     char* name;     struct student* next; }; struct student* ctrn() {     char max[len],a;     struct student * p, * hard, * next;      //p = (struct student*)malloc(sizeof(char));     p = (struct student*)malloc(sizeof(struct student));     if (!p)         exit(0);     p->name = (char *)malloc(sizeof(char) * len);     if (!p->name)         exit(0);     gets_s(max, len);     strcpy(p->name, max);      printf("是否继续输入(y/n):");     scanf_s("%c", &a);     //while (a = 'y')     while (a == 'y')     {         //next= (struct student*)malloc(sizeof(char));         next= (struct student*)malloc(sizeof(struct student));         if (!next)             exit(0);         next->name = (char *)malloc(sizeof(char) * len);         if (!next->name)             exit(0);         gets_s(max, len);         strcpy(next->name, max);         next= next->next;         if (a == 'n')         {             next->next= NULL;             break;         }     }     p->next = next;     hard = p;     while (1)     {         //printf("%d", p->name);         printf("%s", p->name);         p = p->next;         if (p->next != NULL)         {             break;         }     } }  供参考～ if (!p) exit(0); p->name = (char*)malloc(sizeof(char) * len); if (!p->name) exit(0); 这个是什么情况没怎么见过[/quote]  判断内存是否申请成功～[/quote] 好像还是错的，严重性	代码	说明	项目	文件	行	禁止显示状态 警告	C6001	使用未初始化的内存“next”。	Project1	G:\C文件\PROJECT1\PROJECT1\源.C	93	 警告	C6064	缺少“scanf_s”的整型参数(对应于转换说明符“2”)。	Project1	G:\C文件\PROJECT1\PROJECT1\源.C	73	 警告	C28182	取消对 NULL 指针的引用。“p”包含与“p->next”相同的 NULL 值。查看第 100 行以找出可能会发生此情况的前一位置	Project1	G:\C文件\PROJECT1\PROJECT1\源.C	98	 警告	C4473	 “scanf_s”: 没有为格式字符串传递足够的参数	Project1	G:\c文件\Project1\Project1\源.c	73	 错误	C4996	 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.	Project1	G:\c文件\Project1\Project1\源.c	70	 错误	C4996	 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.	Project1	G:\c文件\Project1\Project1\源.c	85

user: user-2
content: 数据结构对单链表进行数据排序 http://bbs.csdn.net/topics/392201633

user: user-3
content: 你这大部分是警告，编译是能过去的，但是要注意一下，有可能会造成程序问题； 两个错误是strcpy不安全，给它换成安全版本就可以了

user: user-0
content: 初学静态链表写的不怎么好，有不规范的都指出来我还能抗

user: user-1
content: 引用 7 楼 Gnglas 的回复:[quote=引用 4 楼 自信男孩的回复:]#include<stdio.h> #include<stdlib.h> #include<string.h> #define len 20 struct student {     char* name;     struct student* next; }; struct student* ctrn() {     char max[len],a;     struct student * p, * hard, * next;      //p = (struct student*)malloc(sizeof(char));     p = (struct student*)malloc(sizeof(struct student));     if (!p)         exit(0);     p->name = (char *)malloc(sizeof(char) * len);     if (!p->name)         exit(0);     gets_s(max, len);     strcpy(p->name, max);      printf("是否继续输入(y/n):");     scanf_s("%c", &a);     //while (a = 'y')     while (a == 'y')     {         //next= (struct student*)malloc(sizeof(char));         next= (struct student*)malloc(sizeof(struct student));         if (!next)             exit(0);         next->name = (char *)malloc(sizeof(char) * len);         if (!next->name)             exit(0);         gets_s(max, len);         strcpy(next->name, max);         next= next->next;         if (a == 'n')         {             next->next= NULL;             break;         }     }     p->next = next;     hard = p;     while (1)     {         //printf("%d", p->name);         printf("%s", p->name);         p = p->next;         if (p->next != NULL)         {             break;         }     } }  供参考～ if (!p) exit(0); p->name = (char*)malloc(sizeof(char) * len); if (!p->name) exit(0); 这个是什么情况没怎么见过[/quote]  判断内存是否申请成功～

user: user-0
content: 引用 4 楼 自信男孩的回复:#include<stdio.h> #include<stdlib.h> #include<string.h> #define len 20 struct student {     char* name;     struct student* next; }; struct student* ctrn() {     char max[len],a;     struct student * p, * hard, * next;      //p = (struct student*)malloc(sizeof(char));     p = (struct student*)malloc(sizeof(struct student));     if (!p)         exit(0);     p->name = (char *)malloc(sizeof(char) * len);     if (!p->name)         exit(0);     gets_s(max, len);     strcpy(p->name, max);      printf("是否继续输入(y/n):");     scanf_s("%c", &a);     //while (a = 'y')     while (a == 'y')     {         //next= (struct student*)malloc(sizeof(char));         next= (struct student*)malloc(sizeof(struct student));         if (!next)             exit(0);         next->name = (char *)malloc(sizeof(char) * len);         if (!next->name)             exit(0);         gets_s(max, len);         strcpy(next->name, max);         next= next->next;         if (a == 'n')         {             next->next= NULL;             break;         }     }     p->next = next;     hard = p;     while (1)     {         //printf("%d", p->name);         printf("%s", p->name);         p = p->next;         if (p->next != NULL)         {             break;         }     } }  供参考～ if (!p) exit(0); p->name = (char*)malloc(sizeof(char) * len); if (!p->name) exit(0); 这个是什么情况没怎么见过