c语言char和str,[求助]C语言问题{关于char *str和char str[100]的区别}

[C] 纯文本查看 复制代码#include

#include

#include

#include

#define N 5

char string[100]; //string是全局的

typedef struct student

{

char name[20];

float score;

struct student *next;

}mm;

mm *build(void)

{

int i=0;

mm *stu,*pstu;

pstu=(mm*)malloc(sizeof(mm));

stu=pstu;

fflush(stdin);

printf("name[%d]:",i);

gets(pstu->name);

printf("score[%d]:",i);

scanf("%f",&pstu->score);

for(i=1;i

{

pstu->next=(mm*)malloc(sizeof(mm));

fflush(stdin);

printf("name[%d]:",i);

gets(pstu->next->name);

printf("score[%d]:",i);

scanf("%f",&pstu->next->score);

pstu->next->next=NULL;

pstu=pstu->next;

}

return stu;

}

void del(mm *head) //函数预期功能:删除链表中第一个跟输入名字符合的结点

{

if(head)

{

char *str; //str没有初始化,但是有值可以打印出来,整个del函数执行过程中str值没有改变,str所指向的字符串值也可以正常打印,gets(str)之后字符串值也没变.

str=string; //看这里,有这句和没这句的区别在哪里,不过是换了个地址而已,执行结果天壤之别{没这句寒风刺骨,有这句春暖花开}

mm *pr,*stu;

printf("\ndelete:\nname:\n");

fflush(stdin);

gets(str);

stu=head;

pr=NULL;

while(strcmp(stu->name,str) !=0 && (stu->next != NULL))

{

pr=stu;

stu=stu->next;

}

if(0==strcmp(stu->name,str))

{

if(pr)

{

pr->next=stu->next;

}

else

{

head=stu->next;

}

}

else

{

printf("there is no log for %s in this table!\n",str);

}

}

else

{

printf("\nthere is no data in this table and nothing to delete!\n");

}

}

void show(mm *stu)

{

int i=0;

mm *pstu;

pstu=stu;

printf("%20s\t\tscore\n","name");

for(i=0;;i++)

{

printf("%20s\t\t%5.1f\n",pstu->name,pstu->score);

if(pstu->next==NULL){break;}

pstu=pstu->next;

}

}

int main(void)

{

mm *head;

head=build();

show(head);

del(head);

show(head);

return 1;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值