cfunctionstruct

Modifying a struct passed as a pointer - C


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;
    }


}

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.

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


Solution

  • Operator Precedence causes

    *article.act_quantity
    

    to be interpreted as *(article.act_quantity)

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