0

how can i use the next struct with typedef by passing it to another function like that?

I have tried this code but i doesn't seem to be working.

typedef struct stru{
    int num;
} stru;

int main(void)
{
    stru current;
    struc(&current);
    printf("%d", current.num);
}


int struc(stru* current)
{
    *current.num = 1;
    return 0;
}

It didn't worked because its not the right way to set it to 1 i guess can someone help me to do it , i have tried few other ways but neither of them is working

*current.num = 1;
4
  • @SLaks well its not workint to set *current.num = 1; by that way Commented Apr 12, 2016 at 21:39
  • 1
    current->num = 1; postfix has higher precedence than unary. Commented Apr 12, 2016 at 21:39
  • What happens? What error do you get? Commented Apr 12, 2016 at 21:39
  • @SLaks Error: exprassion must have struct or union type Commented Apr 12, 2016 at 21:42

1 Answer 1

2

Unary * has less precedence than ..

Therefore, your code is parsed as *(current.num) = 1, which is very much not what you want.

Instead, you need to force the dereference to happen first: (*current).num.

This is so common that C has a shorthand for it: current->num.

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

1 Comment

Oops; the * should have been inside the parentheses.

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.