这可能很简单,但我很难理解它。了解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
的方法中。
有没有'在C. new'操作这是C++。 –
有没有这样的事情,“C代码与'新'”(除非有人在玩一些真正坏的玩笑,并且#把new关键字定义为一个邪恶的宏)。这是C++。 – 2014-01-26 23:29:21
哦,我会更新标题和标签!顺便说一句,谢谢!我本来应该是ANSI-C,先是误解了。 – mrei