0

這可能很簡單,但我很難理解它。瞭解C++代碼'新'和指針

代碼的摘錄,我想了解:

SomeStruct ***namestruct = new SomeStruct **[intVariable]; 
      //Three level pointer and... two level pointer? 
           //or product and pointer? 

for(int i=0; i < intVariable; i++) 
    namestruct[i] = new SomeStruct *[2]; //creates the same structure time 2? 

for(int i=0; i < intVariable; i++) { // just initialization of a 
    namestruct[i][0] = NULL;   // matrix like structure? 
    namestruct[i][1] = NULL; 
} 

代碼工作,但我需要理解爲什麼程序員做了什麼,它的完成。

如何從通過函數作爲地址傳遞的結構的另一個實例分配地址?例如:

void function(SomeStruct **othername); 

int main() 
{ 
    SomeStruct *othername; 
    function(&othername); 
    return 0; 
} 

void function(SomeStruct **othername) 
{ 
    SomeStruct ***namestruct = new SomeStruct **[intVariable]; 
    for(int i=0; i < intVariable; i++) 
    namestruct[i] = new SomeStruct *[2]; 

    for(int i=0; i < intVariable; i++) { 
    namestruct[i][0] = NULL; 
    namestruct[i][1] = NULL; 
    } 

    // This is what I want to do 
    ... 
    namestruct[x][0] = &othername[i]; // Error cannot convert SomeStruct** 
             // to SomeStruct* in assignment 
    ... 
} 

感謝您的幫助!問題出現在代碼本身以及將otherstruct的地址分配給namestruct的方法中。

+0

有沒有'在C. new'操作這是C++。 –

+0

有沒有這樣的事情,「C代碼與'新'」(除非有人在玩一些真正壞的玩笑,並且#把new關鍵字定義爲一個邪惡的宏)。這是C++。 – 2014-01-26 23:29:21

+0

哦,我會更新標題和標籤!順便說一句,謝謝!我本來應該是ANSI-C,先是誤解了。 – mrei

回答

2

我會作一個假設,其實這是你的問題:

我如何分配來自另一個結構體 的地址通過函數作爲地址傳遞?

簡短的回答:

namestruct[x][0] = othername[i]; //if othername is SomeStruct** 

龍答:

SomeStruct ***namestruct = new SomeStruct **[intVariable]; 
// namestruct is a three level pointer, that means you will need three levels 
// of dereferencing to get to an actual value 
// the use of [] notation is the same as a pointer, except you can allocate space 
// in memory. You may recognize the difference between 
// char *someString; //uninitialized, _points_ to 1 available bytes 
// char someString[12]; //uninitialized _points_ to 12 available bytes 
// now, in order to get a value from either of these _pointers_ you use [] 
// as a way of dereferencing 

// the way array dereferencing works is simply that you multiply the sizeof(obj) 
// with the index, and offset your pointer that many bytes 
// such that someString[3] == *(someString + sizeof(char)*3) 

// this means, namestruct is now an 'array' i.e it has memory allocated 
// for intVariable instances, those locations are typed to be SomeStruct ** 


for(int i=0; i < intVariable; i++) 
    namestruct[i] = new SomeStruct *[2]; // since we've learned that * and [] is 
    // almost the same thing, the type SomeStruct *[2] is the same that we expected here 
    // which was SomeStruct ** 
    // namestruct[i][x] will be typed to SomeStruct *, because we dereference two layers 
    // from the original three with the use of the array indexer 

for(int i=0; i < intVariable; i++) { // this is initializing the two pointers generated 
    namestruct[i][0] = NULL;   // in the loop above with new SomeStruct *[2]; 
    namestruct[i][1] = NULL;   // to 0 or NULL 
} 

// You can assign any SomeStruct* to a namestruct[x][y] deref 
// because the types will match. 
+0

非常感謝!你知道,我的編譯器對我起了一個詭計......我嘗試了你之前說過的話並一直強調這個錯誤,然後在你的回答之後我試着再次編譯它,瞧!有時Eclipse Nsight有一些小故障......謝謝! – mrei

3

下面舉例說明你的指針數組的狀態之前有問題的任務:

[SomeStruct***("namestruct")] 
    | 
    V 
    [0:SomeStruct**][1:SomeStruct**][2:SomeStruct**][ ... 
      |    |    | 
      |    |    V 
      |    |    [0:SomeStruct*][1:SomeStruct*] 
      |    |     (NULL)   (NULL) 
      |    V 
      |    [0:SomeStruct*][1:SomeStruct*] 
      |     (NULL)   (NULL) 
      V 
      [0:SomeStruct*][1:SomeStruct*] 
       (NULL)   (NULL) 

[SomeStruct**("othername, function scope")] 
    | 
    V 
    [0:SomeStruct*("othername, main scope")] 

在這一點上,如果你想在葉節點之一分配SomeStruct*指向同一SomeStructothernamemain範圍內,你會做namestruct[x][0] = *othername。請注意,您可以消除指針的額外的水平,如果這是,事實上,你的目標:

SomeStruct *othername; 
function(othername); 
//... 
void function(SomeStruct *othername) 
{ 
    //... 
    namestruct[x][0] = othername; 
+0

對於這樣的代碼,這是很多努力。對於漂亮的圖表+1(沒有驗證過,但是如果你花時間的話,你會知道它是什麼:/) – sehe

+0

@MooseBoys夢幻般的努力。同AlexanderBrevig一樣。 –

+0

@MooseBoys現在我有一個更清晰的圖片!非常感謝! – mrei