編譯器在函數結尾處獲得賦值時發生指針預期錯誤。爲什麼?爲什麼malloc失敗?
(去掉石膏和代碼索引符號;他們在那裏的「調試」買顯然堆滿我的問題。)
int createArraySimple(int initialResetCount, int ***array)
{
int sourceTermIndex, driveCurrentIndex, preEmphasisIndex, freqIndex, voltageIndex, slicerIndex, biasIndex;
int dataIndex, dataCount = 3;
*array = malloc(2*sizeof(int**)); // sourceTerm
if (*array == NULL)
return 0;
for (sourceTermIndex=0; sourceTermIndex < 2; sourceTermIndex++)
{
*((*array)+sourceTermIndex) = malloc(2*sizeof(int*)); // drive current
if (*((*array)+sourceTermIndex) == NULL)
return 0;
for (driveCurrentIndex=0; driveCurrentIndex < 2; driveCurrentIndex++)
{
*((*((*array)+sourceTermIndex))+driveCurrentIndex = malloc(2*sizeof(int)); // pre-emphasis
if (*((*((*array)+sourceTermIndex))+driveCurrentIndex) == NULL)
return 0;
}
}
//'initialize elements in array, since if they are not updated, we won't print them
for (sourceTermIndex = 0; sourceTermIndex < 2; sourceTermIndex++)
for (driveCurrentIndex = 0; driveCurrentIndex < 2; driveCurrentIndex++)
for (preEmphasisIndex = 0; preEmphasisIndex < 2; preEmphasisIndex++)
*((*((*((*array)+sourceTermIndex))+driveCurrentIndex))+preEmphasisIndex) = initialResetCount;
return 1;
}
哇。它的代碼就像這樣可以讓C#成爲一個非常開心的地方。如果我不得不使用這個,我可能會使用一個緩衝區分配,並完成數組[]自己的數學運算,以確保一切正確。 – 2010-09-22 20:29:04
它是全明星! – nategoose 2010-09-22 20:40:48
男人,只是不要發佈'createArrayComplicated'。我認爲我不能接受那個。 – 2010-09-22 20:45:45