为什么这个C代码不起作用?
我试图在每个节点中插入两个随机字符串,但当我打印列表时,输出不正确。可能是什么?我不擅长内存分配,所以如果有什么问题,请向我解释。我还试图查看一个字符串是否覆盖了另一个字符串,但事实似乎并非如此为什么这个C代码不起作用?,c,list,struct,linked-list,dynamic-allocation,C,List,Struct,Linked List,Dynamic Allocation,我试图在每个节点中插入两个随机字符串,但当我打印列表时,输出不正确。可能是什么?我不擅长内存分配,所以如果有什么问题,请向我解释。我还试图查看一个字符串是否覆盖了另一个字符串,但事实似乎并非如此 #include <stdio.h> #include <stdlib.h> #include <string.h> struct node{ int times; char name[100]; char number[100];
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node{
int times;
char name[100];
char number[100];
struct node* next;
};
typedef struct node* node;
void mklist(node* n){
*n=(node)malloc(sizeof(node*));
(*n)->times=0;
strcpy((*n)->name,"null");
strcpy((*n)->number,"null");
(*n)->next=(node)NULL;
}
void listins_beg(node* n,char name[],char num[],int tim){
node a;
a=(node)malloc(sizeof(node));
if(a==NULL) exit(1);
a->times=tim;
strcpy(a->number,num);
strcpy(a->name,name);
a->next=(node)(*n);
(*n)=a;
}
void printlist(node n){
node x;
x=n;
if(x->next==NULL) printf("EMPTY LIST");
else{
do{
printf("%s - %s\n",x->name,x->number);
x=x->next;
}while(x->next!=NULL);
}
}
void freelist(node* n){
node x;
for(;x->next!=NULL;(*n)=(*n)->next){
x=(*n);
free(x);
}
}
int main(void){
node n;
mklist(&n);
listins_beg(&n,"Hermanouhuhuuteu","4523-2248",300);
listins_beg(&n,"Luhu","4523-4887",299);
listins_beg(&n,"Lulamolute","4523-4687",512);
printlist(n);
freelist(&n);
return 0;
}
#包括
#包括
#包括
结构节点{
整数倍;
字符名[100];
字符数[100];
结构节点*下一步;
};
类型定义结构节点*节点;
void mklist(节点*n){
*n=(节点)malloc(sizeof(节点*);
(*n)->次=0;
strcpy((*n)->名称,“空”);
strcpy((*n)->数字,“空”);
(*n)->next=(node)NULL;
}
void listins_beg(node*n,char name[],char num[],int tim){
节点a;
a=(节点)malloc(sizeof(节点));
如果(a==NULL)退出(1);
a->times=tim;
strcpy(a->number,num);
strcpy(a->name,name);
a->next=(节点)(*n);
(*n)=a;
}
无效打印列表(节点n){
节点x;
x=n;
如果(x->next==NULL)printf(“空列表”);
否则{
做{
printf(“%s-%s\n”,x->name,x->number);
x=x->next;
}while(x->next!=NULL);
}
}
无效自由列表(节点*n){
节点x;
对于(;x->next!=NULL;(*n)=(*n)->next){
x=(*n);
免费(x);
}
}
内部主(空){
节点n;
mklist&n;
listins_beg(&n),“Hermanouhuuteu”,“4523-2248”,300);
listins_beg(&n,“路虎”,“4523-4887”,299);
listins_beg(&n,“卢拉莫卢特”,“4523-4687”,512);
打印列表(n);
自由列表(&n);
返回0;
}
首先,正如angew指出的,你需要摆脱
typedef node* node;
您需要回顾结构如何工作的基础知识。例如,您在主菜单中声明
node n;
然后在mklist(..)中尝试分配结构。但是你的声明已经分配了它。如果要分配结构,请声明指针,然后分配结构,并将指针设置为指向刚刚分配的新内存
node *p;
p = malloc(sizeof(node));
您在代码中使用了
typedef struct node*node
,但在函数makelist
和listins\u beg
中使用了
*n=(node)malloc(sizeof(node*));
a=(node)malloc(sizeof(node));
现在这里的*n
是指向结构节点的指针
,但它只分配8字节
或4字节
,这取决于您的机器,因为sizeof(node*)
将返回8或4,因为node*
是指向节点
的指针,为a
分配内存时也会发生同样的情况。应该是这样的
*n=(node)malloc(sizeof(struct node)); //in makelist
a=(node)malloc(sizeof(struct node)); //in listins_beg
typedef结构节点*node代码>不,请不要。这会让你的代码非常混乱。最好不要使用typedef指针,这样语义就会立即可见。但这只是自找麻烦。我尝试过使用typedef struct node
,但它给了我很多错误和麻烦:/使用更多不同的标识符,然后再试一次。在初学者甚至中级程序员级别,很难理解指针。不要隐藏指针涉及的事实,让事情变得更加困难。假设X
是非指针类型,而X*
是指针(对于任何X
)。如果没有*
的帮助,你就不得不考虑什么是指针,什么不是指针。请注意,由于OP的typedef非常混乱和混乱,节点
是结构节点*
的别名。是的,这应该得到修复。我只是不想重复你上面的正确观察。但是,没有提及和应用更改(删除typedef),这个答案是不完整的,因为它不能简单地与OP的代码一起使用。