Avatar of astudent
astudent

asked on 

converting unsigned char ** to unsigned char*

a part of my code looks like:

unsigned char *J;
unsigned char **arr;
unsigned long rows, cols, i, j;
//number of rows, cols is known.

// arr is treated as double dim array and has some values in it,
// spread in rows and columns given by variables rows, cols

//Now i want to transfer values from arr to J
// i.e 2-Dim to 1-Dim.

// Is this ok ?
J=(unsigned char *)malloc(rows*cols* sizeof(unsigned char));

unsigned long x=0;

//start copying
      for(i=0;i<rows;i++)
      {
            printf("%lu", i);
            for(j=0;j<cols;j++)
            {
      
                  *(J+i*cols+j)=  arr[i][j];  //or
                  //*(J+x++)   =  arr[i][j];  //or
                  //J[i*cols+j]=  arr[i][j] ;
            }
      }
      
      printf("\nDone !");



Problem : These loops are not working. Value of i is printed upto 21 and then
it goes berserk, printing some unknown characerers and then
program terminates and even exits IDE without reaching at mesg Done !

What is the problem, what i am doing wrong. Please help.
Thanks.

P.S: Sorry, only 40 points left.
 
C++

Avatar of undefined
Last Comment
astudent

8/22/2022 - Mon