0

I can't understand what's happening. I created a matrix following this post: Function to dynamically allocate matrix

To print the matrix, I created this function:

void PrintMatrix(uint8_t *matrix, size_t nrows, size_t ncols)
{

    // Prints matrix

    size_t i, j;

    printf("\n");

    for (i = 0; i < nrows; ++i) 
    {
            for (j = 0; j < ncols ; ++j) 
            {
            printf("%" PRIu8 "\t", matrix[i][j]);
            }

    printf("\n");
    }

    printf("\n");
}

This function results in a compilation error:

error: subscripted value is neither array nor pointer nor vector
      printf("%" PRIu8 "\t", matrix[i][j]);

In order to check the function, I copied the code inside main() and ran it, suprisingly without any problem, so, why it doesn't work when run as a separated function?

Community
  • 1
  • 1
C. P.
  • 41
  • 1
  • 1
  • 6
  • There is no 2D array (aka matrix) in your code. And C does not support _vectors_. Is this C++? – too honest for this site Dec 26 '16 at 04:19
  • @Olaf. It is a 2D matrix allocated dynamically with malloc (I don't know if that's to be called a 2D matrix). Yes, it's C. – C. P. Dec 26 '16 at 11:12
  • 1) C does not have "matrixes". 2) Matrixes are always 2 dimensional. 3) The C equivalent of a matrix is a 2D array. 4) Your code does not contain a 2D array, but a 1D array (resp. a pointer to the first element). How you allocate the array does not change the type to use. You can very well aloocate a 2D array of arbitrary size with `malloc`. But you need the correct type. A pointer to pointer is a very different datastructure. 5) Not following language rules does not chaange it. C is not very type-safe, but there are still some rules you have to follow. 6) If you want a matrix, use a 2D array. – too honest for this site Dec 26 '16 at 11:22
  • See the answer of haccks. – too honest for this site Dec 26 '16 at 11:24

1 Answers1

1

matrix is of type uint8_t *. It should be of type uint8_t (*)[ncols] if you are passing the array to the function call (which will convert to pointer to array).

Change

void PrintMatrix(uint8_t *matrix, size_t nrows, size_t ncols)

to

void PrintMatrix(size_t nrows, size_t ncols, unit8_t (*matrix)[ncols])
haccks
  • 104,019
  • 25
  • 176
  • 264
  • If so, the function call should be `PrintMatrix(nrows, ncols, matrix)` am I right? – C. P. Dec 25 '16 at 20:26
  • Yes. That's how it should be called. – haccks Dec 25 '16 at 20:36
  • Note: For documentation purposes, it is better to use the equivalent(!) `uint8_t matrix[nrows][ncols]` for the 2D array (i.e. the same as if you declare the array as variable). – too honest for this site Dec 26 '16 at 04:22
  • @Olaf. In this second case is "matrix" still being passed by reference to the "PrintMatrix" function? – C. P. Dec 27 '16 at 19:49
  • @C.P. C does not support pass by reference. It is strictly pass by value. – too honest for this site Dec 28 '16 at 00:23
  • @C.P.: That's why I wrote "equivalent" and mentioned documentation purposes. The semantics are identical; `array` is a pointer, it is just more clear what you expect and what the dimensions are. Also the compiler might be able to apply more checks as can static code checkers and documentation tools like Doxygen can extract more information. All pros, no cons. – too honest for this site Dec 28 '16 at 00:46