2

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

2
  • Read about -> operator. Commented May 30, 2013 at 16:12
  • Hey did u sort out your invalid type argument error? Check in this for any similar mistake. Incase its still not sorted out point out the line. Maybe I can help. And I assume the code in your question is the old one. Commented May 31, 2013 at 7:40

10 Answers 10

11

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)

Sign up to request clarification or add additional context in comments.

1 Comment

@Arlind Seriously, use the arrow (->) syntax, it's much easier.
6

When you reference a pointer to a structure you need either

article->act_quantity

or

(*article).act_quantity

Comments

2

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

Comments

2

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

Comments

2

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

Comments

0

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

Comments

0

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

Comments

0

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

Comments

0
  • 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

Comments

0

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.

2 Comments

What does this add to this answer, posted over 10 years ago?
May be more enlightening for someone. It happens to me that sometimes going through multiple slightly different answers for a given question enlightens me and makes it click.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.