I am trying to use a double pointer to a structure. The build is successful, but on running, it gives the following error:
Run-Time Check Failure #3 – The variable ‘test2’ is being used without being initialized.
The code is:
testStructure* test1 = (testStructure*)malloc(sizeof(testStructure));
testStructure** test2 ;
test1->Integer = 1;
test1->Double = 4.566;
*test2 = test1;
and the structure is:
typedef struct{
int Integer;
double Double;
} testStructure;
Where am I going wrong?
The pointer test1 is a pointer to a structure in the memory. If you need another pointer to the same structure just
testStructure *test2 = test1is sufficient. If you want to modify the address stored in test1 indirectly, then usetestStructure **test2 = &test1so that test2 will be a pointer to the pointer that points to the structure. In your code*test2 = test1tries to access an arbitrary address (initial value of test2) and to set its value to the address of the structure.