This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Convert a pointer to void in a matrix

How can I convert  a dinamic pointer to void, after memcopy in it an array of 1000 bytes, in a matrix of 10x100???

int y;

void* matrix;

memcpy(matrix,buffer,1000);

if i have, for example:

y=(unsigned char)matrix[10][100]; 

it doesn't compile....

  • There are lots of C pointer and array declaration references on the internet. I'm a bit rusty on this type of declaration. I should be something like:

    char (*matrix)[10][100];

    Not sure why need to copy the buffer. Unless buffer gets overwritten. You could leave the data in place.

    char (*matrix)[10][100];
    matrix = buffer;
    y = matrix[9][99]; // Last entry.

    Or do your own arithmetic. Assuming buffer is an array of chars:

    y = buffer[9*100+99]

    Another way is to pass buffer into function. Something like:

    void my_func(unsigned char matrix[10][100])
    {
      int y;
      y = matrix[9][99];
    }

    main()
    {
      unsigned char buffer[1000];
      my_function(buffer);
    }

    In your code fragment, matrix has not been initialized. I assume that you have as10signed some memory to it. Otherwise you might get some problems at runtime.

    I'm sure others will have more elegant or better ways.

    EDIT:

    I think got the row vs column major thing wrong in doing your own arithmatric.

    y = buffer[9 + 99*10]; // same as matrix[9][99]?