简体   繁体   中英

Passing and using struct with typedef

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;

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM