所以我有函数top()返回堆栈的顶部(实现为链表)。它返回一个Node结构。当我尝试访问返回结构的变量时,出现错误。访问返回的结构变量
typedef struct nodeStrcut{
int x,y;
struct nodeStrcut* next;
}Node;
Node top(Node** head){
return **head;
}
void push(Node** head, int x, int y){
//create a new node to contain the value
Node* newPtr = (Node*) malloc(sizeof(Node));
newPtr->x = x;
newPtr->y = y;
newPtr->next = *head;
*head = newPtr;
}
int main(int argc, char **argv){
Node* stack;
stack = NULL;
push(&stack, 3, 3);
push(&stack, 2, 3);
push(&stack, 3, 5);
printf("Node value: %d, %d\n", (pop(&stack)).x, (pop(&stack)).y);
return -1;
}
然后我得到以下错误:
project.c: In function ‘main’:
error: request for member ‘x’ in something not a structure or union
error: request for member ‘y’ in something not a structure or union
我知道,我可以用重新建立了新> X要获取值,但我需要有从的停止返回值的函数叠加。帮助将不胜感激。
你为什么不返回一个指针一个节点? –
'(pop(&stack))。x'->'(top(&stack))。x'? –
你没有在显示的代码中声明'pop()'(你已经定义并因此声明了'top()')。你有不确定的行为,因为你在参数列表中调用了两次'pop()'到'printf()',并且你不能确定调用哪个顺序。'top()'函数可以被安全地调用两次;它会每次都返回相同的值,直到其他内容改变堆栈。 –