我試圖聲明一個指向二維浮點矩陣的指針,以便獲得我的圖像數據的動態行爲,但是Im有一個編譯錯誤C2057:預期的常量表達式。我認爲一個指針必須以這種方式鑄造,但顯然不是。請任何人都可以幫助我?謝謝!!如何聲明一個指向2d浮點矩陣的指針?
//Image size input
int imheight;
int imwidth;
cout << "Please, enter image height: \n>";
scanf ("%d",&imheight);
cout << "Please, enter image width: \n>";
scanf ("%d",&imheight);
const int imheight2 = imheight;
const int imwidth2 = imwidth;
float *zArray[imheight2][imwidth2];
這是我的其他功能之一,我試圖hace訪問zArray。我不是獲取數據正確讀出:
void LoadRIS(char* inputFileName , float** zArray, int imageHeight , int imageWidth){
// Load input RIS file
FILE* lRis = fopen (inputFileName, "rb");
// Jump to data position
for (int i = 0; i < 88; i++){
uchar a = getc (lRis);
}
// Read z array
size_t counter = fread (*zArray , 1 , imageHeight * imageWidth * sizeof(zArray) , lRis);
//Get max value of RIS
float RISmax = zArray [0][0];
float RISmin = zArray [0][0];
for (int i=0; i<imageHeight; i++)
{
for (int j=0; j<imageWidth; j++)
{
if (zArray[i][j] > RISmax)
RISmax = zArray [i][j];
if (zArray[i][j] < RISmin)
RISmin = zArray [i][j];
}
}
std::cout<<"The max value of the RIS file is: "<<RISmax<<"\n";
std::cout<<"The min value of the RIS file is: "<<RISmin<<"\n";
Beep(0,5000);
// Close input file
fclose (lRis);
}
除了回答說些什麼,該類型是指針的二維數組,而不是一個指向二維數組的指針。你會想'float(* zArray)[imheight2] [imwidth2];'。 – 2013-05-06 10:11:24
這個問題被標記爲C和C++,但答案是不同的。 C支持可變長度數組已經有一段時間了,所以小的可變長度數組可以並且應該用'float foo [r] [c];'定義。其中一個標籤應該被刪除。 – 2013-05-06 11:43:37