0

I am writing a program that passes a 2D char array into a function for printing. However, when I try to access elements in the 2D array for printing, I can't. This problem does not occur when I place the printing loops and statements in the main() function, only if the array is passed.

Can someone help explain?

void disp_array(char* array)
{
    for (int i = 0; i < SIZE; i++)
    {
        for (int j = 0; j < SIZE; j++)
        {
            printf("%c", array[i][j]);
        }
        printf("\n");
    }
}

Attached is my code: the j in the printf statement is highlighted with an error:

E0142: "expression must have pointer-to-object type but it has type int"

1 Answer 1

1

The function declaration is wrong.

As the parameter has the type char * then the expressions array[i] yields a scalar object of the type char that is promoted to the type int due to the integer promotions to which you may not apply the subscript operator as you are trying to do array[i][j]. So the compiler issues the error message

E0142: "expression must have pointer-to-object type but it has type int"

The function should be declared like

void disp_array(char array[][SIZE], int n )
{
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < SIZE; j++)
        {
            printf("%c", array[i][j]);
        }
        printf("\n");
    }
}

or like

void disp_array(char (*array)[SIZE], int n )
{
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < SIZE; j++)
        {
            printf("%c", array[i][j]);
        }
        printf("\n");
    }
}

The parameter having the array type char[SIZE][SIZE] is implicitly adjusted by the compiler to the type char ( * )[SIZE]. That is the function does not know how many elements of the type char ( * )[SIZE] exist. So you need to specify the number of elements explicitly. Try always to define a more general function.

And along with the array you need to pass the number of rows in the array. For example

disp_array( array, SIZE );

If the array contains strings then the function definition will look like

void disp_array(char (*array)[SIZE], int n )
{
    for (int i = 0; i < n; i++)
    {
        puts( array[i] );
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! It works now. But may I ask if an array passed like this is passed by value or passed by reference? The reason why I used a pointer initially was so that it could be passed by reference. Some other parts of my program require the array to be passed by reference so that the original array is changed directly (we're not allowed to use global variables).
Why do we need the n parameter, if both dimensions are SIZE?
@ncc1701e To pass by reference in C means passing an object (including arrays) indirectly through pointers to them. That is tp pass your array by reference you need to write the expression &array. In this case the type of the expression will be char ( * )[SIZE][SIZE].
@AdrianMole Because it is a general function. If you specify the parameter like char array [SIZE][SIZE] then it is implicitly converted to the the pointer of the type char ( * )[SIZE] and the function knows nothing that it is an array of the type char[SIZE][SIZE].
Yeah, indeed. Maybe add that to the answer? (Which is a good one!)

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.