c++ - How to declare a pointer to a 2d float matrix? -


im trying declare pointer 2d float matrix in order have dynamical behaviour of image data im having compilation error c2057: expected constant expression. thought pointer had casted in way apparently not.. please me ? thanks!!

    //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]; 

here 1 of other functions i´m trying hace access zarray. im not getting data read:

void loadris( char* inputfilename , float** zarray, int imageheight , int  imagewidth){      // load input ris file file* lris = fopen ( inputfilename, "rb" );  // jump data position (int = 0; < 88; i++){            uchar = 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]; (int i=0; i<imageheight; i++)  {     (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 ris file is: "<<rismax<<"\n"; std::cout<<"the min value of ris file is: "<<rismin<<"\n"; beep(0,5000);   // close input file fclose (lris); 

}

try (dynamical allocation)

//image size input  int imheight; int imwidth;  cout << "please, enter image height: \n>"; scanf ("%d",&imheight); cout << "please, enter image width: \n>"; scanf ("%d",&imwidth);  float** zarray = new float*[imheight]; for(int i=0;i<imheight;i++){     zarray[i] = new float[imwidth]; } 

of course need free allocation by:

for(int i=0;i<imheight;i++){     delete[] zarray[i]; } delete[] zarray; 

hope helps :)

p.s. @frankh says, calls many news , deletes, wasting lot of time. better idea should alloc imwidth*imheight space together.


Comments