gpt4 book ai didi

c - 解引用指针错误

转载 作者:行者123 更新时间:2023-11-30 14:43:33 24 4
gpt4 key购买 nike

下面是 Lib.c 和 Lib.h 文件,它们应该是“电影院”代码的一部分,但我无法获得一些协同工作的指示。

我已经尝试了我认为的每一种组合,但无法弄清楚为什么指针不想合作。

StackLib.h

#include <stdio.h>
#ifndef _STACKLIB_H
#define _STACKLIB_H

struct Node {
char SeatRow;
int SeatNumber;
struct node *next_ptr;
}*StackTop;

void PushSoldSeat(struct Node **StackTop, char RowToAdd, int SeatToAdd);
void PopTickets(struct Node *StackTop);

#endif

.

#include "StackLib.h"
#include <stdio.h>
#include <stdlib.h>

void PushSoldSeat(struct Node **StackTop, char RowToAdd, int SeatToAdd) {
struct Node *NewNode = malloc(sizeof(struct Node));
NewNode->SeatRow = RowToAdd;
NewNode->SeatNumber = SeatToAdd;

if (*StackTop == NULL) {
*StackTop = NewNode;
NewNode->next_ptr = NULL;
} else {
NewNode->next_ptr = *StackTop; //line 20
*StackTop = NewNode;
}
}

void PopTickets(struct Node *StackTop) {
struct Node *TempPtr;
if (StackTop == NULL) {
printf("Pop not executed - stack is empty\n\n");
return;
}
while (StackTop != NULL) {
StackTop = TempPtr;
printf("\n______________\n");
printf("\n| |\n");
printf("\n| Seat %c%d |\n", StackTop->SeatRow, StackTop->SeatNumber);
printf("\n| |\n");
printf("\n______________\n");
TempPtr = StackTop->next_ptr; //line 41
free(StackTop);
}
return;
}

当我 gcc 时,终端打印出:

StackLib.c: In function ‘PushSoldSeat’:
StackLib.c:20: error: dereferencing pointer to incomplete type
StackLib.c: In function ‘PopTickets’:
StackLib.c:41: error: incompatible types in assignment

最佳答案

代码存在一些问题,首先是错误

struct node *next_ptr;

正如@chux所指出的,它应该是struct Node *next_ptr;。

其次,在PopTickets()中打印struct Node信息时,这个

while (StackTop != NULL) { /* some code */ }

应该是

struct Node *TempPtr = StackTop;
while (TempPtr != NULL) { /* some code */ }

因为早期的代码会创建段错误。假设只有一个节点,那么这个

TempPtr = StackTop->next_ptr;

使TempPtrNULL,但StackTop保持不变,并再次循环迭代(不应该),并且这个StackTop = TempPtr; 使 StackTopNULL 并且当它访问 StackTop->SeatRow 时,即 NULL->SeatRow > 它崩溃了。

这里是示例代码:

trevor.c

#include "StackLib.h"
#include <stdio.h>
#include <stdlib.h>
void PushSoldSeat(struct Node **StackTop, char RowToAdd, int SeatToAdd) {
struct Node *NewNode = malloc(sizeof(struct Node));
NewNode->SeatRow = RowToAdd;
NewNode->SeatNumber = SeatToAdd;

if (*StackTop == NULL) {
*StackTop = NewNode;
NewNode->next_ptr = NULL;
}
else {
NewNode->next_ptr = *StackTop; //line 20
*StackTop = NewNode;
}
}

void PopTickets(struct Node *StackTop) {
struct Node *TempPtr = StackTop;
if(StackTop == NULL) {
printf("Pop not executed - stack is empty\n\n");
return;
}
while (TempPtr != NULL) {
StackTop = TempPtr;
printf("\n______________\n");
printf("\n| |\n");
printf("\n| Seat %c%d |\n", StackTop->SeatRow, StackTop->SeatNumber);
printf("\n| |\n");
printf("\n______________\n");
TempPtr = StackTop->next_ptr; //line 41
free(StackTop);
}
}
int main(void) {
struct Node *head = NULL;
PushSoldSeat(&head,'a',5);
PushSoldSeat(&head,'b',10);
PushSoldSeat(&head,'c',15);
PushSoldSeat(&head,'d',20);
PopTickets(head);
return 0;
}

StackLib.h

#include <stdio.h>
#include <stdlib.h>
#ifndef _STACKLIB_H
#define _STACKLIB_H

struct Node {
char SeatRow;
int SeatNumber;
struct Node *next_ptr; /* avoid spell mistake */
}StackTop; /* *stackTop is not required */

void PushSoldSeat(struct Node **StackTop, char RowToAdd, int SeatToAdd);
void PopTickets(struct Node *StackTop);

#endif

现在编译您的代码如下

gcc -Wall -Wstrict-prototypes -Werror trevor.c

关于c - 解引用指针错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53873836/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com