0

I'm wondering is there a way to get the following code to work, or do I have to create a new copy of the function for fixed sizes? If so how can I have a generic function for fixed sizes.

void prettyPrintMatrix_float(float **matrix, int rows, int cols){
    int i, j;

    for (i = 0; i<rows; i++){
        for (j = 0; j<cols; j++){
            printf("%10.3f", matrix[i][j]);
        }
        printf("\n");
    }
    return;
}

float ppArray[4][10] = {0.0f};
prettyPrintMatrix_float(ppArray, 4, 10);

Gives an error Access violation reading location 0xFFFFF....

9
  • 1
    BTW, float[4][10] doesn't decay to float** but (*float)[4]. That is the reason you are getting a violation. Commented Oct 31, 2017 at 14:28
  • 1
    The problem is not passing a fixed-size array to a function, it's passing a two-dimensional array as a pointer to a pointer. float ** is not float[][] Commented Oct 31, 2017 at 14:28
  • @AjayBrahmakshatriya I got that from the warning. But it shouldn't matter in the end because both are saved in the same way in the memory ie as a long 1D array of pointer, right? So is there a way to get around it? Commented Oct 31, 2017 at 14:31
  • 1
    No, a float ** would be a pointer to a pointer, which could be an array of arrays. A float * could technically point to your matrix, but the issue is that you are now dealing with a decayed pointer; one that does not know how many columns would be in the matrix, so it would not know how to index in two dimension, it would only know how to index in one dimension. Commented Oct 31, 2017 at 14:37
  • @ChristianGibbons What does an array of arrays in look like in the memory? I thought it was just a list of pointer each pointer pointing to each row's first element. Commented Oct 31, 2017 at 14:45

3 Answers 3

2

If the compiler supports Variable Length Arrays then you can define the function like

void prettyPrintMatrix_float( int rows, int cols, float matrix[rows][cols] ){
    int i, j;

    for (i = 0; i<rows; i++){
        for (j = 0; j<cols; j++){
            printf("%10.3f", matrix[i][j]);
        }
        printf("\n");
    }
}

Otherwise you can define the function like

void prettyPrintMatrix_float(const float *matrix, int rows, int cols)
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            printf("%10.3f", matrix[i * cols + j]);
        }
        putchar('\n');
    }
}

and call it like

prettyPrintMatrix_float( ( const float * )ppArray, 4, 10);
Sign up to request clarification or add additional context in comments.

8 Comments

I wish. But it does not. So I can't do this. Thanks anyways.
@user14492 Huh, are you using a C++ compiler?
@user14492 I could be wrong here, but i'm pretty sure VLA's are part of the/mandated by the C99 standard
@George you are correct, C99 standard mandates Variable Length Arrays be supported.
are you passing in the -std=c99 flag?
|
0

If Variable Length Arrays are not available to you, and your function will be taking in matrices of varying dimensions, then you're function will have to treat it as a one-dimensional array and manually do the math for calculating where the start of each row is. Something like this:

void prettyPrintMatrix_float(float *matrix, int rows, int cols){
    int i, j;

    for (i = 0; i<rows; i++){
        for (j = 0; j<cols; j++){
            printf("%10.3f", matrix[i * sizeof(float) + j]);
        }
        printf("\n");
    }
    return;
}

float ppArray[4][10] = {0.0f};
prettyPrintMatrix_float(&ppArray[0][0], 4, 10);

Comments

0

Just another solution with templates

template<int rows, int cols>
void prettyPrintMatrix_float(float (&matrix)[rows][cols])
{
    int i, j;
    for(i = 0; i < rows; i++)
    {
        for(j = 0; j < cols; j++)
        {
            printf("%10.3f", matrix[i,j]);
            // Or
            //printf("%10.3f", matrix[i][j]);
        }
        printf("\n");
    }
    return;
}

1 Comment

Just another solution with templates For C?!?!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.