1

I can access array using pointer notion in the function where the array is declared. But, I can not access from another function which throws an error: indirection requires pointer operand ('int' invalid). The code is:

#include <stdio.h>

const int ROW = 3;
const int COL = 3;

void printArray(int *ptr);

int main(void)
{
    int arr[ROW][COL];
    int count = 2;

    // initialize 2D array
    for (int i = 0; i < ROW; i++)
    {
        for (int j = 0; j < COL; j++)
        {
            arr[i][j] = count++;
        }
    }

    // print 2D array
    for (int i = 0; i < ROW; i++)
    {
        for (int j = 0; j < COL; j++)
        {
            // printf("%i\t", arr[i][j]); // using array
            printf("%i\t", *(*(arr + i) + j)); // using pointer

        }
        printf("\n");
    }

    printf("\n---------printing using function------------\n");
    printArray(&arr[0][0]);
}

void printArray(int *ptr)
{
    for (int i = 0; i < ROW; i++)
    {
        for (int j = 0; j < COL; j++)
        {
            printf("%i\t", *(*(ptr + i) + j)); // not working as expected
        }
        printf("\n");
    }
}

How can i solve this problem? any idea?

2 Answers 2

6

*(*(ptr + i) + j) de-refereneces the pointer with two levels of indirection, which isn't possible since it only got one, being an int*.

In general, refrain from crazy pointer arithmetic expressions and use readable array indexing instead: ptr[i + j]. Though your calculation is wrong too, it should be ptr[i*COL + j]


Though the above is referred to as "mangled 2D arrays" and is correct but kind of archaic C. By using pointers to variable-length arrays (VLA) as done in modern C programming, we can turn the code much more readable:

#include <stdio.h>

const int ROW = 3;
const int COL = 3;

void printArray(size_t row, size_t col, int array[row][col]);

int main(void)
{
    int arr[ROW][COL];
    int count = 2;

    // initialize 2D array
    for (int i = 0; i < ROW; i++)
    {
        for (int j = 0; j < COL; j++)
        {
            arr[i][j] = count++;
        }
    }

    // print 2D array
    for (int i = 0; i < ROW; i++)
    {
        for (int j = 0; j < COL; j++)
        {
            // printf("%i\t", arr[i][j]); // using array
            printf("%i\t", *(*(arr + i) + j)); // using pointer

        }
        printf("\n");
    }

    printf("\n---------printing using function------------\n");
    printArray(ROW, COL, arr);
}

void printArray(size_t row, size_t col, int array[row][col])
{
    for (size_t i = 0; i < row; i++)
    {
        for (size_t j = 0; j < col; j++)
        {
            printf("%i\t", array[i][j]);
        }
        printf("\n");
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

For starters pay attention to that in C these declarations

const int ROW = 3;
const int COL = 3;

do not declare integer constants. So your program deals with a variable length array.

If you want to define integer constants you could write for example

enum { ROW = 3, COL = 3 };

Nevertheless either declare and define the function the following way

void printArray( int ( *ptr )[COL], int row );

and

void printArray( int ( *ptr )[COL], int row )
{
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < COL; j++)
        {
            printf("%i\t", *(*(ptr + i) + j));
        }
        printf("\n");
    }
}

and call it like

printArray( arr, ROW );

Or if to use your declaration then the function definition will look like

void printArray(int *ptr)
{
    for (int i = 0; i < ROW; i++)
    {
        for (int j = 0; j < COL; j++)
        {
            printf("%i\t", *( ptr + i * COL + j ) );
        }
        printf("\n");
    }
}

Comments

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.