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