C 为什么这个代码打印00?我能做些什么来修复它?
所以我正在为我的数据结构课程编写一个修订版,所以我决定给自己布置一个作业,让自己熟悉链表。出于某种原因,它打印00。我不知道为什么,但就是这样:C 为什么这个代码打印00?我能做些什么来修复它?,c,C,所以我正在为我的数据结构课程编写一个修订版,所以我决定给自己布置一个作业,让自己熟悉链表。出于某种原因,它打印00。我不知道为什么,但就是这样: #include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct op{ char name[10]; char origin[10]; char ability[20]; int speed; int pick
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct op{
char name[10];
char origin[10];
char ability[20];
int speed;
int pick;
struct op* next;
}op;
void menu();
op* make_node(FILE* fp);
op* append(op* head, op* element);
op* fill_LL(FILE* fp,op* head);
void display_LL(op* head);
int main(void) {
op* head;
head=(op*)malloc(sizeof(op*));
head->next=NULL;
op* element;
element=(op*)malloc(sizeof(op*));
element->next=NULL;
int o;
o = 6;
FILE* fp = fopen("file.txt","r");
//printf("Heal me, Lord, and I will be healed; save me and I will be saved, for you are the one I praise.\n");
append(head,element);
//printf("\nAnd the people all tried to touch him, because power was coming from him and healing them all.\n");
display_LL(head);
//printf("\nBut I will restore you to health and heal your wounds,' declares the Lord.\n");
fclose(fp);
return 0;
}
//simple menu funtion
void menu(){
printf("welcome to the R6S database \n");
printf("Press 1 to get info about an operator");
printf("Press 2 to display the whole database");
printf("Press 3 to add a new operator");
printf("Press 4 to change info about an operator");
printf("Press 5 to sort operators by pick rate and display the ranking");
printf("Press 6 to exit");
}
op* make_node(FILE* fp){
op* element=(op*)malloc(sizeof(op*));
char garbage[10];
fgets(element->name,10,fp);
fgets(element->origin,10,fp);
fgets(element->ability,20,fp);
fscanf(fp,"%d",&element->speed);
fscanf(fp,"%d",&element->pick);
fgets(garbage,10,fp);
return element;
}
op* append(op* head, op* element){
op* walker;
walker=head;
if(head==NULL){
head=element;
}
while (walker->next != NULL){
walker=walker->next;
}
walker->next = element;
return head;
}
op* fill_LL(FILE* fp,op* head){
op* element;
while(!feof(fp)){
element=make_node(fp);
head =append(head,element);
}
return head;
}
void display_LL (op* head){
op* walker;
walker = head;
while (walker->next!=NULL){
//Heal me, Lord, and I will be healed; save me and I will be saved, for you are the one I praise.
//And the people all tried to touch him, because power was coming from him and healing them all.
//But I will restore you to health and heal your wounds,' declares the Lord.
printf("%s", walker->name);
printf("%s", walker->origin);
printf("%s", walker->ability);
printf("%d", walker->speed);
printf("%d", walker->pick);
walker=walker->next;
}
}
未定义的行为:打印对象的值时,没有为其指定任何内容
printf("%s", walker->name); // garbage <empty string>
printf("%s", walker->origin); // garbage <empty string>
printf("%s", walker->ability); // garbage <empty string>
printf("%d", walker->speed); // garbage 0
printf("%d", walker->pick); // garbage 0
printf(“%s”,walker->name);//垃圾
printf(“%s”,walker->origin);//垃圾
printf(“%s”,walker->ability);//垃圾
printf(“%d”,步行者->速度);//垃圾0
printf(“%d”,walker->pick);//垃圾0
首先,您的计算机中没有诅咒。计算机依靠科学技术和数学运行,而不是圣灵。别再想这个了。有时他们会做一些奇怪的事情,但总有一个合乎逻辑的解释
您的代码创建两个节点,并且不在节点中设置任何值。然后,它打印它创建的节点列表。但它不会打印最后一个,因为最后一个节点的next
为空。所以它实际上只是打印第一个节点
第一个节点中似乎只有零。通常,当您分配一些内存时,内存中有随机的垃圾数据,这些数据是以前某件事情遗留下来的,但是可能因为这是您的程序分配的第一件事情,所以您得到了所有的零
名称为空。原点为空。这个能力是空白的。速度是0。选择是0。因此,它在
display\u LL
中打印00.,while(walker->next!=NULL){
应该是while(walker!=NULL){
,否则它不会打印最后一个元素的内容。(我不知道为什么它会输出00
,但代码不会填充任何列表元素,因此它可能打印任何旧的垃圾。)是的,你是对的,我还没有完成代码。head=(op*)malloc(sizeof(op*);
应该是head=(op*)malloc(sizeof(op));
但是类型转换malloc
的结果是不必要的,所以head=malloc(sizeof(op));
是首选的,或者更好,head=malloc(sizeof(*)head)
。element=(op*)malloc(sizeof(op*));
。00
输出可能来自printf(“%d”,walker->speed);
和printf(“%d”,walker->pick);
如果malloced内存恰好初始化为全零。当我用append(head,make_node(fp))调用append函数时
它打印结构的前三个元素,然后是一个分段错误。但00仍然在开头。“计算机中没有诅咒”,除了!:)
printf("%s", walker->name); // garbage <empty string>
printf("%s", walker->origin); // garbage <empty string>
printf("%s", walker->ability); // garbage <empty string>
printf("%d", walker->speed); // garbage 0
printf("%d", walker->pick); // garbage 0