On arduino I have some problems, I don't really understand.
A simple example of just one variable referencing to another variable.
In C
int *p; // reference
int a;
p = &a;
// print p - address
// print &p - address of memory location 'p'
// print *p - dereferencing : returns value on that address
// 'a' MUST BE initialized and then 'p' allocated to 'a' ####
a = 10;
p = &a;
print p - address
print &p - address of memory location 'p'
print *p - dereferencing : returns value on that address
On the arduino
int a = 1;
int *b = &a;
*b += 1;
//now a == 2 (and b's value is a pointer to the location of a in memory)
Serial.print("a - ");
Serial.println(a, DEC );
Serial.print("b - ");
Serial.print(*b, DEC ); // dereferencing
So I have the ERROR error: call of overloaded 'println(int*&, int)' is ambiguous if I try
Serial.print(b, DEC ); // address of 'b'
// OR
Serial.print(&b, DEC ); // address of memory location 'p'
My original intension is to make a swap function using reference
/*
Variables
*/
const long SERIAL_SPEED=38400;//baud rate of the serial communication
struct rect {
int width;
int height;
};
void swap(struct rect *input);
void swap(struct rect *input) {
int tmp = input->width;
input->width = input->height;
input->height = tmp;
}
void setup() {
Serial.begin(SERIAL_SPEED); // open the serial port at SERIAL_SPEED bps:
struct rect first;
first.width = 111;
first.height = 222;
Serial.println( "\n -- BEFORE -- ");
Serial.print( " - first.width : "); Serial.println(first.width, DEC);
Serial.print( " - first.height : "); Serial.println(first.height, DEC);
swap($first);
Serial.println( "\n -- AFTER -- ");
Serial.print( " - first.width : "); Serial.println(first.width, DEC);
Serial.print( " - first.height : "); Serial.println(first.height, DEC);
error: call of overloaded 'println(int*&, int)' is ambiguous
Well, evidently Serial.println doesn't take those arguments. It won't print out a pointer to int - not on the arduino at least. Try casting the pointer to an int.
void setup() {
// put your setup code here, to run once:
int a = 1;
int *b = &a;
*b += 1;
Serial.begin(115200);
//now a == 2 (and b's value is a pointer to the location of a in memory)
Serial.print("a - ");
Serial.println(a, DEC );
Serial.print("b - ");
Serial.println(*b, DEC ); // dereferencing
Serial.println( (int) b, DEC ); // NO! This is the content of the pointer variable b
// OR
Serial.println( (int) &b, DEC ); // address of memory location where b is stored
Serial.println( (int) &a, DEC ); // address of memory location where a is stored
}
void loop() {
// put your main code here, to run repeatedly:
}
I ran this code on a Mega 2560 Pro Mini the output is:
a - 2
b - 2
8697
8695
8697
Keep in mind that the content of b (i.e., its rvalue), since it is a properly-initialized pointer, should be the memory address of variable a. Any pointer variable should only hold one of two values: 1) NULL, in which case it points to no valid data, or 2) a memory address. This says the rvalue of b is 8697 and that variable b "lives" at memory address 8695 (i.e., the lvalue of b). The third number is where variable a lives in memory (i.e., the lvalue of a). The numbers tell you that the rvalue of b is the lvalue of a. That is, b points to where a lives in memory. Since b knows where variable a lives in memory, b can change its value "indirectly" using the process of indirection.
Read that paragraph about 50 times and it will make sense.
arduino_314:
In C++ you can pass the parameter by:
Value
Reference
Pointer
That’s interesting. Having not yet jumped into learning C++, I did not know that. The way I keep it straight in my head for plain old C is that parameters are ALWAYS passed to functions by VALUE. That value may be the value of a pointer variable, so it’s a reference to another variable. But, it’s the VALUE of whatever was inside the (..) of the calling statement.
econjack:
Keep in mind that the content of b (i.e., its rvalue), since it is a properly-initialized pointer, should be the memory address of variable a. Any pointer variable should only hold one of two values: 1) NULL, in which case it points to no valid data, or 2) a memory address. This says the rvalue of b is 8697 and that variable b "lives" at memory address 8695 (i.e., the lvalue of b). The third number is where variable a lives in memory (i.e., the lvalue of a). The numbers tell you that the rvalue of b is the lvalue of a. That is, b points to where a lives in memory. Since b knows where variable a lives in memory, b can change its value "indirectly" using the process of indirection.
Read that paragraph about 50 times and it will make sense.