关于链表的问题
来源:百度知道 编辑:UC知道 时间:2024/09/12 21:11:21
我照书抄了代码,编译时有这样的问题
E:\w.cpp(32) : warning C4700: local variable 'del_char' used without having been initialized
但再编译一遍却显示
Compiling...
Skipping... (no relevant changes detected)
w.cpp
w.obj - 0 error(s), 0 warning(s)
我用的是vc6.0,运行后
当我输入要删除的station名称后,就出了这样一个问题
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
int num=0;
struct station
{
char name[8];
struct station *next;
};
struct station *create_list(struct station *h);
void printf_station(struct station *h);
struct station *del_station(struct station *h,char *str);
void main()
{
char* del_char;
struct station *head;
head=NULL;
printf("please input station name:\n");
head=create_list(head);
printf("number of station =%d\n",num);
printf_station(head);
E:\w.cpp(32) : warning C4700: local variable 'del_char' used without having been initialized
但再编译一遍却显示
Compiling...
Skipping... (no relevant changes detected)
w.cpp
w.obj - 0 error(s), 0 warning(s)
我用的是vc6.0,运行后
当我输入要删除的station名称后,就出了这样一个问题
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
int num=0;
struct station
{
char name[8];
struct station *next;
};
struct station *create_list(struct station *h);
void printf_station(struct station *h);
struct station *del_station(struct station *h,char *str);
void main()
{
char* del_char;
struct station *head;
head=NULL;
printf("please input station name:\n");
head=create_list(head);
printf("number of station =%d\n",num);
printf_station(head);
刚跟你调试过了,错误是局部变量' del_char '使用,而没被初始化,你在定义的时候是char* del_char;实际上他是野指针,没有指向任何地址,当然是错误的,所以在定义的时候你在主函数中直接将char *del_char;改成char del_char[10];问题就解决了。
所以在以后的学习中一定要注意指针的初始化,而且在编程的过程中你要学会读懂错误的原因,其实编译环境已经提示你错误在什么地方,如果你知道什么意思,这个错误其实很好改的是不是嘛,如果实在翻译不来就将这句话复制到谷歌翻译上去,看看什么意思,然后在自己改正错误,只有这样你才能提高你的编程能力,你说呢,希望对你有所启示
其实就是一个问题,你的del_char没有分配空间,所以你调用scanf("%s", del_char)输入字符串的时候有错误,改成char del_char[32] = {0};;