C 我在实现堆栈数据结构时遇到分段错误
我在下面的代码片段中遇到了一个分段错误。它是一种代码,用于为交易卡中的各种属性赋值并显示。我通常会搞砸数据结构,所以如果你们能建议一些资源来学习分段错误和类似的东西,这将非常有帮助C 我在实现堆栈数据结构时遇到分段错误,c,segmentation-fault,stack,C,Segmentation Fault,Stack,我在下面的代码片段中遇到了一个分段错误。它是一种代码,用于为交易卡中的各种属性赋值并显示。我通常会搞砸数据结构,所以如果你们能建议一些资源来学习分段错误和类似的东西,这将非常有帮助 #include<stdio.h> #include<stdlib.h> typedef struct cards { int power; int energy; int heal; int karma; struct cards *next; }
#include<stdio.h>
#include<stdlib.h>
typedef struct cards
{
int power;
int energy;
int heal;
int karma;
struct cards *next;
}node;
node *createCards()
{
node *new_node=(node*)malloc(sizeof(node));
new_node->energy=500+rand()%400;
new_node->heal=100+rand()%200;
new_node->karma=50+rand()%100;
new_node->power=1000+rand()%501;
return new_node;
}
void createStack(node *head, int no_cards)
{
if(head==NULL)
head=createCards();
head->next=NULL;
int i=0;
while(i<no_cards-1)
{
node *tmp=createCards();
tmp->next=head;
head=tmp;
i++;
}
}
void displayCards(node *head)
{
node *crt=head;
int i=1;
while(crt->next)
{
printf("\n ------------------------------------- ");
printf("\n | <%d> |", i);
printf("\n | |");
printf("\n | POWER : %d |", crt->power);
printf("\n | |");
printf("\n | |");
printf("\n | ENERGY: %d |", crt->energy);
printf("\n | |");
printf("\n | |");
printf("\n | HEAL : %d |", crt->heal);
printf("\n | |");
printf("\n | |");
printf("\n | KARMA : %d |", crt->karma);
printf("\n | |");
printf("\n -------------------------------------");
i++;
crt=crt->next;
}
}
node *player1=NULL;
int main()
{
createStack(player1, 10);
displayCards(player1);
}
#包括
#包括
typedef结构卡
{
整数幂;
国际能源;
治疗;
内因业力;
结构卡*下一步;
}节点;
节点*createCards()
{
node*new_node=(node*)malloc(sizeof(node));
新建节点->能量=500+rand()%400;
新建节点->修复=100+rand()%200;
新建节点->业力=50+rand()%100;
新建节点->功率=1000+rand()%501;
返回新的_节点;
}
void createStack(节点*头部,内部无卡)
{
if(head==NULL)
head=createCards();
head->next=NULL;
int i=0;
而(不精确=头部;
水头=tmp;
i++;
}
}
无效显示卡(节点*头)
{
节点*crt=头部;
int i=1;
while(crt->next)
{
printf(“\n--------------------------------------------------------”;
printf(“\n | |”,i);
printf(“\n | |”);
printf(“\n |电源:%d |”,crt->电源);
printf(“\n | |”);
printf(“\n | |”);
printf(“\n |能量:%d |”,阴极射线管->能量);
printf(“\n | |”);
printf(“\n | |”);
printf(“\n |治疗:%d |”,crt->HEAL);
printf(“\n | |”);
printf(“\n | |”);
printf(“\n |业力:%d |”,crt->KARMA);
printf(“\n | |”);
printf(“\n--------------------------------------------------------”;
i++;
crt=crt->next;
}
}
node*player1=NULL;
int main()
{
createStack(player1,10);
显示卡(玩家1);
}
我怀疑您想编写createStack()
函数,如
void createStack (node *head, int no_cards) {
if (head == NULL) {
head = createCards();
}
int i = 0;
node *tmp = head;
while(i < no_cards) {
tmp->next = createCards();
tmp = tmp->next;
i++;
}
}
void createStack(节点*头部,内部无卡){
if(head==NULL){
head=createCards();
}
int i=0;
节点*tmp=头部;
而(我<没有卡){
tmp->next=createCards();
tmp=tmp->next;
i++;
}
}
我注意到您正在创建一个链表,而不是堆栈。
并删除函数displayCards()
中while循环底部的行crt->next
。
谢谢你的提问:)
另外,如果您有任何其他疑问,请发表评论。我将编辑我的答案来回答这个问题。由于您将
player1
的副本传递给createStack
并且此函数将指针分配给一个局部变量,因此一旦函数返回,它将丢失,因此player1
变量永远不会被分配分配内存player1
保持NULL
状态,因此出现了segfault。若要从createStack
修改player1
,您需要传递其地址,或者更改createStack
以返回新头并将其分配给调用者中的player1
。非常感谢你们的输入。双重取消引用变量头(即传递的player1)带来了所有的不同,因为函数得到了一个实际参数,而不是一个副本。非常感谢您的建议。函数中的双重取消引用解决了这个问题,因为函数得到了要处理的实际参数,而不是传递函数的副本。