Is (*exptr)->cnt the same as exptr->cnt or (*exptr).cnt?

  • A+

Is (*pointer)->name the same as pointer->name or (*pointer).name?


In C, the a->b operator is a shorthand for (*a).b.

struct foo {     int b; };  // the . operator is used when the struct object is NOT a pointer struct foo a; a.b = 42;  // the -> operator is used when the struct object IS a pointer struct foo *a = malloc(sizeof *a); a->b = 42;  // the same thing as the above example, but using the dot operator (*a).b = 42; 

The last example is dereferencing the a pointer (getting the object it points to), then using the dot operator to access the element b inside of it. Now let's translate your question.

// first one (*a)->b;  // would be the same as: (*(*a)).b; // that is: (**a).b; // which would be used in struct foo **a ... ; (**a).b; // get the first element of the array, access field b   // second example a->b; // is the same as (*a).b; // which is your third example 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: