修改作为一个指针传递一个结构 - Ç [英] Modifying a struct passed as a pointer - C

查看:193
本文介绍了修改作为一个指针传递一个结构 - Ç的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图修改被作为参数传递使用指针由我不能让它工作的一个结构。我不能只返回结构,因为函数必须返回一个整数。我怎样才能修改功能的结构?这是我迄今所做的:

I'm trying to modify a struct that was passed as a parameter by using a pointer by I can't get it working. I cant just return the struct because the function must return an integer. How can I modify the struct in the function? This is what I've done so far:

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的错误:::请求成员中的所有行,我试图修改结构

I get this error: "error: request for member: 'act_quantity' in something not a structure or union' " in all the lines where I tried to modify the struct.

我得到这个错误。

编辑:我曾用。代替 - >。我现在固定它。它仍然给我的错误:一元的无效类型参数'*'(有'诠释')

I had used "." instead of "->". I fixed it now. It still gives me an error: " invalid type argument of unary '*' (have 'int')"

推荐答案

操作precedence 导致

*article.act_quantity

是PTED为 *(article.act_quantity)

这应该是(*文章).act_quantity 物品─&GT; act_quantity (当LHS是指针)

It should be (*article).act_quantity or article->act_quantity (when the LHS is a pointer)

这篇关于修改作为一个指针传递一个结构 - Ç的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆