-4

I have an exam question question here which asks:

"The C operator -> is a shorthand for a more complex notation. Explain the circumstances in which -> or the more complex notation would be used. Write an example of the more complex notation."

I`m not sure what the examiner is looking for here. My impression is that there is only one way to denote a structure dereference and that is ->. Can anyone help?

ajq88
  • 305
  • 1
  • 14
  • 1
    How would you dereference an object (not to access a member; just dereference it)? If you have an object (not a pointer), how do you access a member? – Cornstalks Apr 07 '14 at 17:09
  • 1
    Do some research before asking here, you clearly know that it is shorthand for dereference ... so how could we figure out the other notation... [Google is your friend](https://www.google.com/search?num=20&safe=off&site=&source=hp&q=c%20dereference&=&oq=&gs_l=) – clcto Apr 07 '14 at 17:09

2 Answers2

0

Dereferencing can be done either by using -> or (*).. See the example

struct point {
  int x;
  int y;
};
struct point my_point = { 3, 7 };
struct point *p = &my_point;  /* To declare and define p as a pointer of type struct point,
                                 and initialize it with the address of my_point. */

(*p).x = 8;                   /* To access the first member of the struct */
p->x = 8;    

Most probably your teacher was asking for (*p).x = 8;.

haccks
  • 104,019
  • 25
  • 176
  • 264
0

The -> syntax is a shorthand for (*). syntax, so struct->member is equivalent to (*struct).member.

Parenthesis are mandatory because of operator priority, basically here the member-access (.) operator has a greater priority than the dereferencing (*) operator.

Remember, programmers are lazy and often like shortcuts.

Chnossos
  • 9,971
  • 4
  • 28
  • 40