Skip to main content
added 86 characters in body
Source Link
jfpoilpret
  • 9.2k
  • 7
  • 38
  • 54
    const long SERIAL_SPEED=38400;//baud rate of the serial communication

//global variable
    int a = 15;
    int *b=a;
    
void infoVar(){
    Serial.print("\n\n");
    Serial.print(" a -  ");  Serial.println(a, DEC );
    Serial.print("*b -  ");  Serial.println(*b, DEC );  // dereferencing
    Serial.print("\n");

    Serial.print("int  b - ");  Serial.println( (int) b, DEC );   // ???
    // This is the content of the pointer variable b
    Serial.print("int &b - ");  Serial.println( (int) &b, DEC );  // address of memory location where b is stored
    Serial.print("int &a - ");  Serial.println( (int) &a, DEC );  // address of memory location where a is stored 
}
    
void setup() {
  // put your setup code here, to run once:
    Serial.begin(SERIAL_SPEED);      // open the serial port at SERIAL_SPEED bps:

    Serial.println("Started : ### reference_pointer ###\n\n");


// *** :  when uncomment next line : the value b = 123
//b=123

    *b=a;
    infoVar();
    
    a=235;
    *b=a;
    infoVar();
    
    a=1928;
    *b=a;
    infoVar();
    
    }

void loop() {
  // put your main code here, to run repeatedly:
}


 
OUTPUT

a - 15 *b - 15

int b – 15 // don't understand here that it is 15 //thought that is would be the the address of the variable 'a' // it seems that it keeps the first initiated value??? *** int &b - 676 int &a - 256

a - 235 *b - 235

int b - 15 int &b - 676 int &a - 256

a - 1928 *b - 1928OUTPUT

int b - 15 int &b - 676 int &a - 256

 a -  15
*b -  15

int  b – 15
        // don't understand here that it is 15
            //thought that is would be the the address of the variable 'a'
            // it seems that it keeps the first initiated value??? ***
int &b - 676
int &a - 256


 a -  235
*b -  235

int  b - 15
int &b - 676
int &a - 256


 a -  1928
*b -  1928

int  b - 15
int &b - 676
int &a - 256
    const long SERIAL_SPEED=38400;//baud rate of the serial communication

//global variable
    int a = 15;
    int *b=a;
    
void infoVar(){
    Serial.print("\n\n");
    Serial.print(" a -  ");  Serial.println(a, DEC );
    Serial.print("*b -  ");  Serial.println(*b, DEC );  // dereferencing
    Serial.print("\n");

    Serial.print("int  b - ");  Serial.println( (int) b, DEC );   // ???
    // This is the content of the pointer variable b
    Serial.print("int &b - ");  Serial.println( (int) &b, DEC );  // address of memory location where b is stored
    Serial.print("int &a - ");  Serial.println( (int) &a, DEC );  // address of memory location where a is stored 
}
    
void setup() {
  // put your setup code here, to run once:
    Serial.begin(SERIAL_SPEED);      // open the serial port at SERIAL_SPEED bps:

    Serial.println("Started : ### reference_pointer ###\n\n");


// *** :  when uncomment next line : the value b = 123
//b=123

    *b=a;
    infoVar();
    
    a=235;
    *b=a;
    infoVar();
    
    a=1928;
    *b=a;
    infoVar();
    
    }

void loop() {
  // put your main code here, to run repeatedly:
}


 
OUTPUT

a - 15 *b - 15

int b – 15 // don't understand here that it is 15 //thought that is would be the the address of the variable 'a' // it seems that it keeps the first initiated value??? *** int &b - 676 int &a - 256

a - 235 *b - 235

int b - 15 int &b - 676 int &a - 256

a - 1928 *b - 1928

int b - 15 int &b - 676 int &a - 256

    const long SERIAL_SPEED=38400;//baud rate of the serial communication

//global variable
    int a = 15;
    int *b=a;
    
void infoVar(){
    Serial.print("\n\n");
    Serial.print(" a -  ");  Serial.println(a, DEC );
    Serial.print("*b -  ");  Serial.println(*b, DEC );  // dereferencing
    Serial.print("\n");

    Serial.print("int  b - ");  Serial.println( (int) b, DEC );   // ???
    // This is the content of the pointer variable b
    Serial.print("int &b - ");  Serial.println( (int) &b, DEC );  // address of memory location where b is stored
    Serial.print("int &a - ");  Serial.println( (int) &a, DEC );  // address of memory location where a is stored 
}
    
void setup() {
  // put your setup code here, to run once:
    Serial.begin(SERIAL_SPEED);      // open the serial port at SERIAL_SPEED bps:

    Serial.println("Started : ### reference_pointer ###\n\n");


// *** :  when uncomment next line : the value b = 123
//b=123

    *b=a;
    infoVar();
    
    a=235;
    *b=a;
    infoVar();
    
    a=1928;
    *b=a;
    infoVar();
    
    }

void loop() {
  // put your main code here, to run repeatedly:
}

OUTPUT

 a -  15
*b -  15

int  b – 15
        // don't understand here that it is 15
            //thought that is would be the the address of the variable 'a'
            // it seems that it keeps the first initiated value??? ***
int &b - 676
int &a - 256


 a -  235
*b -  235

int  b - 15
int &b - 676
int &a - 256


 a -  1928
*b -  1928

int  b - 15
int &b - 676
int &a - 256
Source Link

@MATT, with trying to get the address of variable via

    const long SERIAL_SPEED=38400;//baud rate of the serial communication

//global variable
    int a = 15;
    int *b=a;
    
void infoVar(){
    Serial.print("\n\n");
    Serial.print(" a -  ");  Serial.println(a, DEC );
    Serial.print("*b -  ");  Serial.println(*b, DEC );  // dereferencing
    Serial.print("\n");

    Serial.print("int  b - ");  Serial.println( (int) b, DEC );   // ???
    // This is the content of the pointer variable b
    Serial.print("int &b - ");  Serial.println( (int) &b, DEC );  // address of memory location where b is stored
    Serial.print("int &a - ");  Serial.println( (int) &a, DEC );  // address of memory location where a is stored 
}
    
void setup() {
  // put your setup code here, to run once:
    Serial.begin(SERIAL_SPEED);      // open the serial port at SERIAL_SPEED bps:

    Serial.println("Started : ### reference_pointer ###\n\n");


// *** :  when uncomment next line : the value b = 123
//b=123

    *b=a;
    infoVar();
    
    a=235;
    *b=a;
    infoVar();
    
    a=1928;
    *b=a;
    infoVar();
    
    }

void loop() {
  // put your main code here, to run repeatedly:
}



OUTPUT

a - 15 *b - 15

int b – 15 // don't understand here that it is 15 //thought that is would be the the address of the variable 'a' // it seems that it keeps the first initiated value??? *** int &b - 676 int &a - 256

a - 235 *b - 235

int b - 15 int &b - 676 int &a - 256

a - 1928 *b - 1928

int b - 15 int &b - 676 int &a - 256