I had noticed that the address of the pointer was the same for each call of the function given in the examples. It then dawned on me the reason was the address displayed was for the function parameter pointer, not the pointer being passed to the function.
There is nothing wrong with the examples and they work as intended. When I first viewed the code, I thought the intent was to display the address of the pointer that was passed, so this caused some confusion for a few minutes. The results were not matching what I expected. Only when I looked more closely did I realize my mistake. Hopefully this will help someone else.
I modified the code in the examples to provide address of the pointer being passed rather than the address of the parameter. This code was written in an Arduino Sketch.
void pointerFuncA_Original(Print* p, const char* str, int *iptr) {
p->printf("Value of %s: %d\n", str, *iptr);
p->printf("Address of %s: %p\n", str, iptr);
p->printf("Address of pointer to %s: %p\n", str, &iptr);
}
void pointerFuncA(Print* p, const char* str, int **iptr) {
p->printf("Value of %s: %d\n", str, *(*iptr));
p->printf("Address of %s: %p\n", str, *iptr);
p->printf("Address of pointer to %s: %p\n", str, &(*iptr));
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
// Wait for the serial port to connect
while (!Serial) ;
int i = 1234;
int *fooi = &i;
int j = 5678;
int *fooj = &j;
Serial.println("--- Original Function outputs the same address for the pointer passed to the function. ---");
pointerFuncA_Original(&Serial, "i", fooi);
Serial.println();
pointerFuncA_Original(&Serial, "j", fooj);
Serial.println();
Serial.println("--- Modified Function Calls showing correct address of the pointer passed to the function. ---");
pointerFuncA(&Serial, "i", &fooi);
Serial.println();
pointerFuncA(&Serial, "j", &fooj);
Serial.println();
Serial.println("--- Verify ---");
Serial.printf("Value of i: %d\n", i);
Serial.printf("Address of i: %p\n", &i);
Serial.printf("Address of fooi: %p\n", &fooi);
Serial.println();
Serial.printf("Value of j: %d\n", j);
Serial.printf("Address of j: %p\n", &j);
Serial.printf("Address of fooj: %p\n", &fooj);
}
void loop() {
// put your main code here, to run repeatedly:
}
OUTPUT:
--- Original Function Calls same address the pointer sent to the function. ---
Value of i: 1234
Address of i: 0x20041fb8
Address of pointer to i: 0x20041fa4
Value of j: 5678
Address of j: 0x20041fc0
Address of pointer to j: 0x20041fa4
--- Modified Function Calls showing correct address of the pointer sent to the function. ---
Value of i: 1234
Address of i: 0x20041fb8
Address of pointer to i: 0x20041fbc
Value of j: 5678
Address of j: 0x20041fc0
Address of pointer to j: 0x20041fc4
--- Verify ---
Value of i: 1234
Address of i: 0x20041fb8
Address of fooi: 0x20041fbc
Value of j: 5678
Address of j: 0x20041fc0
Address of fooj: 0x20041fc4
The original version displays the address of the pointers to i & j as 0x20041fa4. The modified version and the verification check show the correct addresses of the pointers to i & j that were passed to the function.