C 访问返回的结构变量
所以我使用函数top()返回堆栈的顶部(实现为链表)。它返回一个节点结构。尝试访问返回的结构的变量时出错C 访问返回的结构变量,c,C,所以我使用函数top()返回堆栈的顶部(实现为链表)。它返回一个节点结构。尝试访问返回的结构的变量时出错 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 t
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
我知道我可以使用stack->x来获取值,但我需要有一个从堆栈停止处返回值的函数。我们将不胜感激 您不需要在
top()
中传入指向指针的指针。将函数定义从节点顶部(节点**头部)
更改为节点顶部(节点*头部)
,就足够了
现在,在调用以下内容时,无需传入堆栈的地址(无拼写错误):
您不需要在top()
中传入指向指针的指针。将函数定义从节点顶部(节点**头部)
更改为节点顶部(节点*头部)
,就足够了
现在,在调用以下内容时,无需传入堆栈的地址(无拼写错误):
我想这只是一个输入错误(pop
而不是top
),这样您实际上调用了一个库函数,而没有返回节点的类型。写入printf(“节点值:%d,%d\n”,顶部和堆栈).x,顶部和堆栈.y)代码>应该可以正常工作。我想这只是一个输入错误(pop
,而不是top
),这样您实际上调用了一个库函数,而没有返回节点的类型。写入printf(“节点值:%d,%d\n”,顶部和堆栈).x,顶部和堆栈.y)代码>并且它应该按预期工作
Node* newPtr = (Node*) malloc(sizeof(Node));
不需要强制执行malloc的返回,这是不必要的。请参阅:。以下几点就足够了:
Node *newPtr = malloc (sizeof *newPtr);
head
的地址在top
中没有更改,因此不需要传递head
的地址,例如
Node top (Node *head){
return *head;
}
您不应该从main()
返回负值。有两种定义的回报:
EXIT_SUCCESS: 0
EXIT_FAILURE: 1
总而言之:
#include <stdio.h>
#include <stdlib.h>
typedef struct nodeStrcut{
int x,y;
struct nodeStrcut* next;
} Node;
Node top (Node *head){
return *head;
}
void push (Node **head, int x, int y) {
Node *newPtr = malloc (sizeof *newPtr);
newPtr->x = x;
newPtr->y = y;
newPtr->next = *head;
*head = newPtr;
}
int main (void) {
Node *stack = NULL;
push (&stack, 3, 3);
push (&stack, 2, 3);
push (&stack, 3, 5);
printf ("Node value: %d, %d\n", (top (stack)).x, (top (stack)).y);
return 0;
}
不需要强制执行malloc的返回,这是不必要的。请参阅:。以下几点就足够了:
Node *newPtr = malloc (sizeof *newPtr);
head
的地址在top
中没有更改,因此不需要传递head
的地址,例如
Node top (Node *head){
return *head;
}
您不应该从main()
返回负值。有两种定义的回报:
EXIT_SUCCESS: 0
EXIT_FAILURE: 1
总而言之:
#include <stdio.h>
#include <stdlib.h>
typedef struct nodeStrcut{
int x,y;
struct nodeStrcut* next;
} Node;
Node top (Node *head){
return *head;
}
void push (Node **head, int x, int y) {
Node *newPtr = malloc (sizeof *newPtr);
newPtr->x = x;
newPtr->y = y;
newPtr->next = *head;
*head = newPtr;
}
int main (void) {
Node *stack = NULL;
push (&stack, 3, 3);
push (&stack, 2, 3);
push (&stack, 3, 5);
printf ("Node value: %d, %d\n", (top (stack)).x, (top (stack)).y);
return 0;
}
为什么不返回指向节点的指针呢?(pop(&stack)).x
->(top(&stack)).x
?在显示的代码中您没有声明pop()
(您已经定义并因此声明了top()
)。您有不确定的行为,因为您在参数列表中调用了两次pop()
,以printf()
,并且无法判断调用的顺序。top()
函数可以安全调用两次;每次它都会返回相同的值,直到其他内容更改堆栈。显示pop函数?为什么不返回指向节点的指针?(pop(&stack)).x
->(top(&stack)).x
?您没有在显示的代码中声明pop()
(您已经定义并因此声明了top()
)。您有不确定的行为,因为您在参数列表中调用了两次pop()
,以printf()
,并且无法判断调用的顺序。top()
函数可以安全调用两次;每次它都将返回相同的值,直到其他内容更改堆栈。显示pop函数?并且您应该能够使用top(&stack).x
,而不使用额外的括号。并且您应该能够使用top(&stack).x
,而不使用额外的括号。