c - 修改作为指针传递的结构 - C

标签 c function struct

我试图通过使用指针来修改作为参数传递的结构,但我无法使其正常工作。我不能只返回结构,因为该函数必须返回一个整数。如何修改函数中的结构?这是我到目前为止所做的:

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')的无效类型参数”

最佳答案

Operator Precedence原因

*article.act_quantity

被解释为*(article.act_quantity)

应该是(*article).act_quantity或者article->act_quantity(当LHS是指针时)

关于c - 修改作为指针传递的结构 - C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16841018/

相关文章:

c - 如何使用命令行中的 argv 参数(在 C 中)打开文件并循环遍历文件行?

c++ - 可由设备或主机调用的 CUDA 函数

python - 如何从C共享库返回无符号字符数组来调用python函数

c - 'sizeof (function name)' 返回什么?

python - 将函数作为参数传递给其他函数 python

c - 如何读入文件,然后将文件中的每个结构放入数组中?

arrays - Matlab:如何生成 0x0 结构数组?

c - 如何将 char 转换为 unsigned int?

c - 为什么C编译器不接受这个条件赋值?

c - 在 C 中使用动态分配将文件读入多维数组