这是我的 C 代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct node * ptr;
typedef struct node {
int data;
ptr next;
} item;
void printList(ptr p);
int main() {
ptr h = NULL;
}
void printList(ptr p) {
while(p) {
printf("%d -> \n", ptr -> data);
ptr = ptr -> next;
}
}
编译代码时出现 2 个错误:
error: unexpected type name 'ptr': expected expression
printf("%d -> \n", ptr -> data);
^
error: expected identifier or '('
ptr = ptr -> next;
我尝试尝试各种更改,但我不明白为什么会出现这些错误。
最佳答案
你指的是结构成员的类型而不是结构成员。
printf("%d -> \n", ptr -> data);
ptr = ptr -> next;
应该是
printf("%d -> \n", p->data);
p = p->next;
关于c - 为什么无法识别我的链表类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41859156/