Arlind
Arlind

Reputation: 434

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')"

Upvotes: 2

Views: 9636

Answers (10)

Aliy
Aliy

Reputation: 1

it is only a matter of precedence. In C, the '*' operator has less precedence than the dot '.' operator, but parentheses () have the highest precedence in C. Thus you need to add parentheses the following way (*article).act_quantity.

Upvotes: 0

and
and

Reputation: 31

This should fix the problem

int update_article(elem *article, int sold)
{
    if(article->act_quantity >= sold)
    {
        article->act_quantity = article->act_quantity - sold;
        if(article->act_quantity < article->reorder_threshold)
        {
            strcpy(article->note, "to reorder");
            return -1;
        }
        else
            return 0;
    }
    else if(article->act_quantity < sold)
    {
        strcpy( article->note, "act_quantity insufficient");
        return -2;
    }
}

Upvotes: 2

kotlomoy
kotlomoy

Reputation: 1430

  • It still gives me an error: " invalid type argument of unary '*' (have 'int')"

Because you write *article->act_quantity instead of article->act_quantity. Fix it

Upvotes: 0

Grzegorz Piwowarek
Grzegorz Piwowarek

Reputation: 13773

You don't have to return a pointer to the struct because it will still remain the same as the input. You pass address to the function and you work with something that is at this address. No need to return anything connected with the struct in this case

article is a pointer therefore You can't just use article.act_quantity and You should replace . with ->

article->act_quantity

Upvotes: 0

unwind
unwind

Reputation: 399763

This:

strcpy(*article.note, "act_quantity insufficient");

won't work, note is a character array, you can't derference it. You need:

strcpy(article->note, "act_quantity insufficient");

Upvotes: 0

akhil
akhil

Reputation: 732

Use arrow operators to access members of a structure using structure pointer. But valid memory must be there.

Upvotes: 0

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84159

Change *article.act_quantity to (*article).act_quantity, or better to article->act_quantity, etc. Operator precedence gets you here ...

Upvotes: 2

Suvarna Pattayil
Suvarna Pattayil

Reputation: 5239

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)

Upvotes: 11

Sodved
Sodved

Reputation: 8588

When you reference a pointer to a structure you need either

article->act_quantity

or

(*article).act_quantity

Upvotes: 6

user405725
user405725

Reputation:

It should be ptr->member, not a ptr.member when dealing with pointers.

Upvotes: 2

Related Questions