我试图通过使用指针来修改作为参数传递的结构,但是我无法使其工作.我不能只返回结构,因为该函数必须返回一个整数.如何修改函数中的结构?这是我到目前为止所做的:
typedef enum {TYPE1, TYPE2, TYPE3} types;
typedef struct {
types type;
int act_quantity;
int reorder_threshold;
char note[100];
}elem;
int update_article(elem *article, int sold)
{
if(*article.act_quantity >= sold)
{
article.act_quantity = article.act_quantity - sold;
if(article.act_quantity < article.act_quantity)
{
strcpy(article.note, "to reorder");
return -1;
}
else
return 0;
}
else if(article.act_quantity < venduto)
{
strcpy(*article.note, "act_quantity insufficient");
return -2;
}
}
Run Code Online (Sandbox Code Playgroud)
我得到这个错误:"错误:请求成员:'act_quantity'在某些东西中不是结构或联合'"在我尝试修改结构的所有行中.
编辑:我用过"." 而不是" - >".我现在修好了.它仍然给我一个错误:"一元'*'的无效类型参数(有'int')"
Suv*_*yil 10
运算符优先级原因
*article.act_quantity
Run Code Online (Sandbox Code Playgroud)
被解释为 *(article.act_quantity)
应该是(*article).act_quantity或article->act_quantity(当LHS是指针时)
当您引用指向结构的指针时,您也需要
article->act_quantity
Run Code Online (Sandbox Code Playgroud)
要么
(*article).act_quantity
Run Code Online (Sandbox Code Playgroud)