You are not logged in.
Hi, I'm encountering a problem that I can best describe with this code:
#include "xbasic_types.h" #include "xstatus.h" #include "xio.h" #include <stdlib.h> #include <stdio.h> #include <string.h> #include <math.h> #include "xparameters.h" #include "xuartlite_l.h" #define NumberOfTestStructs 10 struct TestStruct { int value; }; static struct TestStruct TestStructArray[NumberOfTestStructs]; void SetPtr(struct TestStruct TestStructArray[], struct TestStruct* TestStructPtr); int main() { struct TestStruct* TestStructPtr; xil_printf("\n\r\n\rStarting Test Struct program"); TestStructArray[0].value = 100; SetPtr(TestStructArray, TestStructPtr); // Try commenting/uncommenting this next line xil_printf("\n\rTest Struct Array [0]'s address: %d", &(TestStructArray[0])); // If the previous line were not called, then the address here would be different from the address in the function xil_printf("\n\rTest Struct Pointer's pointing to address: %d", TestStructPtr); // If the address in the line above is different from the address in the line above, then this next line will not return the correct value (100) xil_printf("\n\rTest Struct Pointer's value: %d", TestStructPtr->value); xil_printf("\n\r"); while(1) XUartLite_RecvByte(STDIN_BASEADDRESS); } void SetPtr(struct TestStruct TestStructArray[], struct TestStruct* TestStructPtr) { TestStructPtr = &(TestStructArray[0]); xil_printf("\n\rTest Struct Array [0]'s address: %d", &(TestStructArray[0])); xil_printf("\n\rTest Struct Pointer's pointing to address: %d", TestStructPtr); xil_printf("\n\rTest Struct Pointer's pointing to address's value: %d", TestStructPtr->value); }
As you can see in the comments in between the lines of code, if I run the program as is, HyperTerminal would display the correct value of 100 for TestStructPtr->value. But if I comment out that line that references TestStructArray[0]'s address, the value that is printed out is not correct and the value of the TestStructPtr is different from the one displayed by the function SetPtr. Can someone explain why this is? It seems that TestStructPtr will actually change its address value if I reference another variable.
Thanks,
Joshua
Offline
Nevermind, it is resolved. Turns out I need to use a pointer to a pointer for the function.
Offline