我有一個相當奇怪的問題。我想初始化一個由void指針指向的數組,使用new分配內存,如下所示。現在動態數組和空指針
const int ARRAY_SIZE = 10;
void InitArray()
{
int *ptrInt = new int[ARRAY_SIZE];
for(int i=0; i<ARRAY_SIZE;i++)
{
ptrInt[i] = 1; //OK
}
void *ptrVoid = new int[ARRAY_SIZE];
for(int i=0; i<ARRAY_SIZE;i++)
{
*(int*)ptrVoid[i] = 1; //Culprit : I get a compiler error here
//(error C2036: 'void *' : unknown size)
}
}
,我要初始化這個數組是由ptrVoid
尖說1.如何去了解它的要素是什麼?有了這段代碼,我得到了一個編譯器錯誤,如代碼中所示(我正在使用VS 2010)。有什麼建議麼?
不使用'void *'。 – 2011-03-23 01:09:04
不要使用'new []'。使用'std :: vector'。 – GManNickG 2011-03-23 06:01:11
爲什麼在表達式中一次全部完成?給自己一個新的intptr,併爲其分配你的voidptr的值。對於最隱蔽的代碼沒有額外的要點! – 2011-03-23 17:58:47