【发布时间】:2023-03-11 01:03:01
【问题描述】:
我试图声明一个指向二维浮点矩阵的指针,以便我的图像数据具有动态行为,但我遇到了编译错误 C2057:预期的常量表达式。我认为必须以这种方式投射指针,但显然不是......请任何人都可以帮助我吗?谢谢!!
//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];
这是我尝试访问 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];。 -
这个问题被标记为 C 和 C++,但答案不同。 C 支持可变长度数组已有一段时间了,因此可以并且应该使用
float foo[r][c];定义小的可变长度数组。应该删除其中一个标签。