C中指向结构的指针
我正在使用指针和结构在C中实现堆栈。Push和Stack函数工作良好(它们都在内存中创建新元素)。无论如何,pop函数不起作用,我不知道为什么,下面是该函数的代码:C中指向结构的指针,c,pointers,struct,C,Pointers,Struct,我正在使用指针和结构在C中实现堆栈。Push和Stack函数工作良好(它们都在内存中创建新元素)。无论如何,pop函数不起作用,我不知道为什么,下面是该函数的代码: int pop(element **lastStackEl) { int poppedValue = *lastStackEl->value; element *temp = *lastStackEl->prev; free(*lastStackEl); *lastStackEl=temp; retu
int pop(element **lastStackEl)
{
int poppedValue = *lastStackEl->value;
element *temp = *lastStackEl->prev;
free(*lastStackEl);
*lastStackEl=temp;
return poppedValue;
}
error: request for member 'value' in something not a structure or union
int poppedValue = *lastStackEl->value;
这是我的结构:
typedef struct Element {
int value;
struct Element *prev;
} element;
编译器在pop函数的第一行和第二行中给出错误:
int pop(element **lastStackEl)
{
int poppedValue = *lastStackEl->value;
element *temp = *lastStackEl->prev;
free(*lastStackEl);
*lastStackEl=temp;
return poppedValue;
}
error: request for member 'value' in something not a structure or union
int poppedValue = *lastStackEl->value;
根据,间接(取消引用)运算符(*
)晚于成员访问运算符(->
)。因此,如果没有显式括号,您的语句的行为就像
int poppedValue = *(lastStackEl->value);
int poppedValue = (*lastStackEl)->value;
现在,lastStackEl
是指向元素的指针
它不能用作成员访问操作符的LHS。这就是错误消息的全部内容
您需要先解除限制lastStackEl
(以获取元素*
类型),然后可以使用->
访问成员值
。你应该写