2012-02-10 130 views
-1

我想用雙指針結構。構建成功,但在運行時,它提供了以下錯誤:雙指針結構

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; 

我要去哪裏錯了?

回答

0

指針TEST1是指向在存儲器中的結構。如果你需要另一個指向相同結構的指針,那麼只需要testStructure *test2 = test1即可。如果要間接修改test1中存儲的地址,則使用testStructure **test2 = &test1,以便test2將指向指向該結構的指針。在你的代碼*test2 = test1試圖訪問任意地址(test2的初始值)並將其值設置爲結構的地址。

13
*test2 = test1; // test2 is pointing no where to get dereferenced. 

必須是

test2 = &test1; 
1

需要test2 = &test1 - 即TEST2是指針的地址TEST1