I am learning C and am having trouble passing the pointer of a 2D array to another function that then prints the 2D array. Any help would be appreciated.
int main( void ){     char array[50][50];     int SIZE;      ...call function to fill array... this part works.      printarray( array, SIZE ); }  void printarray( char **array, int SIZE ){     int i;     int j;      for( j = 0; j < SIZE; j++ ){         for( i = 0; i < SIZE; i ++){             printf( "%c ", array[j][i] );         }         printf( "\n" );     } } 
                // The following program works only if your compiler is C99 compatible. If compiler is not C99 compatible, then we can use one of the following methods to pass a variable sized 2D array. In this method, we must typecast the 2D array when passing to function.
Array can be passed to function in C using pointers and because they are passed by reference changes made on an array will also be reflected on the original array outside function scope.
A whole array cannot be passed as an argument to a function in C++. You can, however, pass a pointer to an array without an index by specifying the array's name.
char ** doesn't represent a 2D array - it would be an array of pointers to pointers.  You need to change the definition of printarray if you want to pass it a 2D array:
void printarray( char (*array)[50], int SIZE )   or equivalently:
void printarray( char array[][50], int SIZE ) 
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With