用'new'和指针理解C ++代码

时间:2014-01-26 23:27:43

标签: c++ pointers function-pointers argument-passing assignment-operator

这可能很简单,但我很难理解它。

我想要了解的代码的摘录:

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的方法。

2 个答案:

答案 0 :(得分: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*以指向SomeStruct范围内与othername相同的main,会做namestruct[x][0] = *othername。请注意,如果这实际上是您的目标,则可以消除额外的指针级别:

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

答案 1 :(得分: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.