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?