简体   繁体   中英

Passing a pointer to struct as a parameter to a function in C

When passing a pointer to struct as a parameter to a function what is the point of using that parenthesis (*t).name in the printf .

I'm confused about what is the difference between (*t).name , *(t).name and t.name .

typdef struct{
 const char *name;
 }phone;

 void update(phone *t){
 printf("Name %s!,(*t).name);
 }

The function doesn't take the struct as a parameter, it takes a pointer to the struct. In order to use a pointer, you have to dereference it, and *ptr is used to dereference a pointer to access the object it points to.

(*t).name

is equivalent to:

t->name

which is the more common way to write it.

t.name

can't be used because t is not a structure, it's a pointer, and . can only be used with a structure.

*(t).name

is wrong because . has higher precedence than * , so it's equivalent to:

*(t.name)

I suggest you go back to your textbook or tutorial, and reread the chapter on pointers.

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