what the -> operand does?

Hello C++ experts!

I've a question about the -> operand that I now see quite often in some code for embedded systems. it looks to me to be anotehr way to access the fields of a given structure, but why using this operator istead of using the "." which I was teached from school? is there any difference?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 // for example I have this function :
static void configure_systick_handler(void)
{
	SysTick->CTRL = 0;
	SysTick->LOAD = 999;
	SysTick->VAL  = 0;

}

where SysTick is a strucure of SysTick_Type 

typedef struct
{
  __IO uint32_t CTRL;                    /*!< Offset: 0x000 (R/W)  SysTick Control and Status Register */
  __IO uint32_t LOAD;                    /*!< Offset: 0x004 (R/W)  SysTick Reload Value Register       */
  __IO uint32_t VAL;                     /*!< Offset: 0x008 (R/W)  SysTick Current Value Register      */
  __I  uint32_t CALIB;                   /*!< Offset: 0x00C (R/ )  SysTick Calibration Register        */
} SysTick_Type;


 Put the code you need help with here.
You use the -> when you have a pointer, a '.' when you have an actual object.

1
2
3
4
//This
ptr->member = 0;
//Is just shorthand for
(*ptr).member = 0;


This is necessary because a pointer itself is nothing other than an address. You have to dereference the pointer to grab the object it points to before you can start accessing members and methods.
In addition to what ResidentBiscuit said, -> is an operator, not an operand.

In ptr->member, ptr and member are operands, and -> is the operator.

Last edited on
Given something like:
1
2
3
4
5
6
7
8
9
10
11
struct D
{
    int first;
    int second;
}

D* ptrToD;

// These are equivalent
ptrToD->first;
(*D).first;
Hello Guys!

Thanks a lot for the clarifications!
Could you also point me some manuals/tutorials that explain such concepts?

it's quite weird that I do not really rember to have studied the usage of -> in my c++ book of ten years ago (...or so!)

BR
Could you also point me some manuals/tutorials that explain such concepts?

What concepts? Points are explained on this site's tutorials. http://www.cplusplus.com/doc/tutorial/pointers/
Topic archived. No new replies allowed.