I’m in the process of modifying the Tire Pressure Monitoring System code to allow configuration information to be retrieved and saved to an SD card as well as making use of the three buttons that I added to my version. I had an issue with the display not initializing as expected. Upon further investigation, I realized that it was an issue with the comparison of two objects, with one being the SPI bus for the CC1101 radio module and the other being the SPI bus for the display. The code initializes the CC1101 SPI bus by default as the CC1101 SPI is required. The code then should initialize the display SPI bus if it is being used for the display and is not the same as the CC1101 SPI bus.
The SPI busses for the CC1101 and display are different, but the check passed on the display SPI bus initialization because the values are the same. I needed to look at the addresses to see if they are pointing to the same location. I used some code that I found on StackOverflow. When I pulled some of the code to look at the values and addresses, I was left scratching my head as it looked like they had the same value, the addresses they were pointing to were different, but the addresses of the pointers were the same. I scratched my head for a few minutes then it dawned on me that the example code was actually displaying the address of the function parameter and not the address of the pointer. Once I figured that out, I started gaining my sanity back. I decided to add a response to the post with some modified code to show what I was expecting. Hopefully it will help someone later if they run into the same issue.
The StackOverflow post is at https://stackoverflow.com/questions/32914298/print-value-and-address-of-pointer-defined-in-function. Below is the reply that I added.
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.
You must be logged in to post a comment.