双指针结构
问题描述:
我想用双指针结构。构建成功,但在运行时,它提供了以下错误:双指针结构
Run-Time Check Failure #3 - The variable 'test2' is being used without being initialized.
的代码是:
testStructure* test1 = (testStructure*)malloc(sizeof(testStructure));
testStructure** test2 ;
test1->Integer = 1;
test1->Double = 4.566;
*test2 = test1;
和结构是:
typedef struct{
int Integer;
double Double;
} testStructure;
我要去哪里错了?
答
指针TEST1是指向在存储器中的结构。如果你需要另一个指向相同结构的指针,那么只需要testStructure *test2 = test1
即可。如果要间接修改test1中存储的地址,则使用testStructure **test2 = &test1
,以便test2将指向指向该结构的指针。在你的代码*test2 = test1
试图访问任意地址(test2的初始值)并将其值设置为结构的地址。
答
*test2 = test1; // test2 is pointing no where to get dereferenced.
必须是
test2 = &test1;
答
需要test2 = &test1
- 即TEST2是指针的地址TEST1