我试图通过使用指针来修改作为参数传递的结构,但我无法使其正常工作。我不能只返回结构,因为该函数必须返回一个整数。如何修改函数中的结构?这是我到目前为止所做的:
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;
}
}
我在尝试修改结构的所有行中收到此错误:“错误:请求成员:'act_quantity' in something not a structure or union'”。
编辑:我用过“。”而不是“->”。我现在修好了。它仍然给我一个错误:“一元'*'(具有'int')的无效类型参数”
最佳答案
*article.act_quantity
被解释为*(article.act_quantity)
应该是(*article).act_quantity
或者article->act_quantity
(当LHS是指针时)
关于c - 修改作为指针传递的结构 - C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16841018/