-> operator

Hello,

please, can someone navigate me (google is not helping), what is operator “->” used for?

like:

this->setAddress2(0x20);

Sound strange, but I can not find anything about this…

Thanks

"this" is a pointer to the current object, and setAddress2 is a member function. So, for "this" object, call setAddress2 with 32 as a parameter.

Thanks for quick reaction and guiding me.

It's usually used to dereference a struct or object. So if you have a pointer to a struct, sPtr, then this:

sPtr->myVariable

is the same as:

(*sPtr).myVariable

Google member access operator C++

example: let's say we have structure 'myStruct' and let's say 'pinNumber' was a member in the structure.

You can access the member using: myStruct.pinNumber ( note the period . )

If 'this' is a pointer, you can set it to the address of a function. struct test myStruct, *this=&myStruct;

These then would be equivalent: myStruct.pinNumber this->pinNumber (*this).pinNumber

.

Hello,

I need a little help understanding a line of code and am failing at finding a solution. Basically I’d just like to know what this line of code means:

RTC->MODE2.INTFLAG.bit.ALARM0=1; //clearing alarm0 flag

I’m mainly just confused about the ‘->’ operator. If someone could explain it to me and tell me what that operator is called it would be a huge help.

The full code is here:

/***************************************************************************************************************************************************************************
* This sketch demonstrate how to use alarm in interrupt mode.
This mode is more conveniently because you use processor for other tasks and when alarm match occurs interrupt routine is executed.
In this way, alarm flag checking is indipendent from main program flow.
****************************************************************************************************************************************************************************/
//*RTC Alarm in interrupt mode*/
#include <RTCInt.h> //include RTCint library
 
RTCInt rtc;         //create an RTCInt type object
int buzzer=11;      //connect the buzzer to digital pin 11
int red=12;         //connect red led to digital pin 12
int green=13;       //connect green led to digital pin 13
/*setup*/
void setup() 
{
  SerialUSB.begin(9600);    //serial communication initializing
  pinMode(green,OUTPUT);    //define green as output
  pinMode(red,OUTPUT);      //define red as output
  pinMode(buzzer,OUTPUT);   //define buzzer as output
  digitalWrite(green,LOW);  //initialize the green to LOW level
  digitalWrite(red,LOW);    //initialize the green to LOW level
  rtc.begin(TIME_H24);      //RTC initializing with 24 hour representation mode
  rtc.setTime(17,0,5,0);    //setting time (hour minute and second)
  rtc.setDate(13,8,15);     //setting date
  rtc.enableAlarm(SEC,ALARM_INTERRUPT,alarm_int); //enabling alarm in interrupt mode 
  rtc.local_time.hour=17;   //setting hour alarm
  rtc.local_time.minute=5;  //setting minute alarm
  rtc.local_time.second=10; //setting second to match
  rtc.setAlarm();           //write second in alarm register
}
/*loop*/
void loop()
{  noTone(buzzer);           // disable the buzzer
   digitalWrite(green,HIGH); //turn on green led
   delay(500);               //wait 500 millisecond 
}
 
/*************** Interrupt routine for alarm ******************************/
void alarm_int(void)
{
  SerialUSB.println("Alarm match!");
    for(int i=0; i < 30; i++)
    { digitalWrite(green,LOW);       //turn off green led
      tone(buzzer,200);              //play buzzer
      digitalWrite(red,HIGH);        //turn on red green
      for(int j=0; j < 1000000; j++) asm("NOP");  //in interrupt routine you cannot use delay function then an alternative is NOP instruction cicled many time as you need
      digitalWrite(red,LOW);         //turn off red green italWrite(13,LOW);
      for(int j=0; j < 2000000; j++) asm("NOP");
    }
    RTC->MODE2.INTFLAG.bit.ALARM0=1; //clearing alarm0 flag
}

check C++ reference doc on pointers

In this statement:

RTC->MODE2.INTFLAG.bit.ALARM0=1; //clearing alarm0 flag

posts #3 and #4 explain what the '->' is about. In object oriented languages, a class object contains two basic things: 1) variables (members of the object), and 2) functions (methods of the object). Think of the dot operator ('.') as a key that lets you open the object and gain access to the object's members and methods. The fly in the ointment here is that one class object can contain other objects if it needs to. So, in your case, you might say:

"RTC is a pointer to a MODE2 object, so I'll use that pointer to find MODE2 in memory. Using my key (i.e., the dot operatot) to get inside the MODE2 object, I find it contains an INTFLAG object. So let me use another key to get inside the INTFLAG object. Once inside the INTFLAG object, I find another object (perhaps a structure) name bit. So, I pull out another key to gain access to that object's variable named ALARM0 and I assign that variable the value of 1."

This example is like one of those Russian puzzle boxes that contains objects within objects within objects... I once worked with a (horribly-designed) database library that routinely used 13 levels of objects within objects. It might help you to think of the dot operator as a key that opens the door or an object, structure, or enum.

Thanks econjack that actually really clears it up for me! One last thing I’m trying to figure out:

Shouldn’t ‘RTC’ be defined somewhere?

I’d expect something like:

struct MODE2 RTC_SETUP1; //Or something like that to create this structure in memory
RTC = &RTC_SETUP1;

fore4runner: Thanks econjack that actually really clears it up for me! One last thing I'm trying to figure out:

Shouldn't 'RTC' be defined somewhere?

I'd expect something like:

struct MODE2 RTC_SETUP1; //Or something like that to create this structure in memory
RTC = &RTC_SETUP1;

Yes, RTC could be assigned that way, but it also has to be declared:

struct Mode2 RTC_SETUP1; // create instancel
...
struct Mode2 *RTC; // declare pointer
RTC=&RTC_SETUP1; // assign pointer

Also, some header files will define the object, often near the bottom of the header file.

econjack: Also, some header files will define the object, often near the bottom of the header file.

+1. It's done for you with the perhaps reasonable assumption that there will only ever be one RTC in your system.