首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

为什么插入不了数据呢?解决方案

2012-03-17 
为什么插入不了数据呢???#includestdio.h #includestring.h #includestdlib.h typedefstructsNode{c

为什么插入不了数据呢???
#include   "stdio.h "
#include   "string.h "
#include   "stdlib.h "

typedef   struct   sNode   {
char   num[8];
char   name[12];
char   zy[20];
int   score;
char   sex;
struct   sNode*   next;
}*LinkList,LNode;

//1、在头部插入
LinkList   Creat_LinkList1()
{
LinkList   L=NULL;
LNode   *s;
char   num[8],name[12],zy[20],sex;
int   score;
scanf( "%s,%s,%c,%s,%d ",num,name,&sex,zy,&score);
while   (strcmp(num,0)!=0)
{
s=malloc(sizeof(LNode));
strcpy(s-> num,num);
strcpy(s-> name,name);
s-> sex=sex;
strcpy(s-> zy,zy);
s-> score=score;
s-> next=L;L=s;
scanf( "%s,%s,%c,%s,%d ",num,name,&sex,zy,&score);
}
return   L;
}

void   main()
{
printf( "请输入学生信息,以学号0作为结束点。\n ");
printf( "学号     姓名     性别     专业     入学成绩\n ");
        Creat_LinkList1();
}




[解决办法]
while (strcmp(num,0)!=0)
{
s=malloc(sizeof(LNode));//改为s=(LNode *)malloc(sizeof(LNode));
strcpy(s-> num,num);
strcpy(s-> name,name);
s-> sex=sex;
strcpy(s-> zy,zy);
s-> score=score;
s-> next=L;L=s;
scanf( "%s,%s,%c,%s,%d ",num,name,&sex,zy,&score);
}
[解决办法]
错误1:s=malloc(sizeof(LNode));应该为s=(LNode *)malloc(sizeof(LNode));
错误2:while (strcmp(num,0)!=0);应该为while (strcmp(num, "0 ")!=0)
注意是字符串比较,所以需要对0加双引号
[解决办法]
strcmp用错了,0指针没有储存空间
函数名: strcmp
功 能: 串比较
用 法: int strcmp(char *str1, char *str2);
程序例:

#include <string.h>
#include <stdio.h>

int main(void)
{
char *buf1 = "aaa ", *buf2 = "bbb ", *buf3 = "ccc ";
int ptr;

ptr = strcmp(buf2, buf1);
if (ptr > 0)
printf( "buffer 2 is greater than buffer 1\n ");
else
printf( "buffer 2 is less than buffer 1\n ");

ptr = strcmp(buf2, buf3);
if (ptr > 0)
printf( "buffer 2 is greater than buffer 3\n ");
else
printf( "buffer 2 is less than buffer 3\n ");

return 0;
}

热点排行