下面是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
答案 0 :(得分:1)
代码很少有问题,首先是错误在这里
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;
将TempPtr
设为NULL
,但StackTop
保持不变,然后再次循环迭代(不应该这样),并且此StackTop = TempPtr;
使StackTop
成为{{ 1}},当它访问NULL
即StackTop->SeatRow
时就会崩溃。
这是示例代码:
trevor.c
NULL->SeatRow
StackLib.h
#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;
}
现在按如下所示编译代码
#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
答案 1 :(得分:0)
大写字母对C很重要。您有一个struct node
,它是您定义的。在内部,它有一个指向不同类型gcc
的指针,编译器对此一无所知。您应该使用一致的大写字母,并且应该更好用
顺便说一句,我希望struct node
在.h文件中打印一条警告,在此您首次引用新的-Wall -Wextra -pedantic
。请务必使用{{1}}进行编译,这样可以帮助您捕获很多错误。