4
#include <stdio.h>

void f(int *app[][20]) {
    int i, j;
    for (i =0; i< 20; i++){
        for (j=0; j<20; j++){
            *app[i][j] = i;
        }
    }
}

int main() {
  int app[20][20];
  int i, j;
  f(&app);
    for (i =0; i< 20; i++){
        for (j=0; j<20; j++){
            printf("i %d, j%d  val= %d\n", i, j, app[i][j]);
        }
    }

  return 0;
}

What exactly am I doing wrong here? I don't get any error, but there is a segmentation fault and I don't know why.

te.c:15:5: warning: passing argument 1 of ‘f’ from incompatible pointer type
   f(&app);
     ^
te.c:3:6: note: expected ‘int * (*)[20]’ but argument is of type ‘int (*)[20][20]’
 void f(int *app[][20]) {
tandem
  • 2,040
  • 4
  • 25
  • 52
  • why are you trying to run code that does not cleanly compile? – user3629249 Apr 30 '15 at 20:38
  • I ignored it as it was a warning and tried to run. Since it was a pointer error, it was bound to be a seg. fault. that's when I asked this question. Thanks! – tandem May 02 '15 at 07:56

3 Answers3

6
void f(int *app[][20]) { /* a 2d array of pointers to int */

should be

void f(int app[][20]) { /* a pointer to an array of 20 int's */

or

void f(int (*app)[20]) { /* a pointer to an array of 20 int's */

*app[i][j] = i;

should be

app[i][j] = i; /* you don't need to dereference */

f(&app);

should be

f(app);
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • Question of curiosity: When passing a value as a pointer to a function we do `f(&i)` and use it as `void f (int *i)`. Why not here? – tandem Apr 29 '15 at 10:36
  • When you pass an `int` to a function using `int i; f(i);` you are passing the value, when you use `int i; f(&i);` you are passing a reference to the variable – David Ranieri Apr 29 '15 at 10:40
  • Why can't we pass by reference here? – tandem Apr 29 '15 at 10:43
  • 1
    Because an array decays into a pointer when is passed to a function, in other words: `int a[][20];` decays into `int (*a)[20];` (a pointer to an array of 20 `int`'s) – David Ranieri Apr 29 '15 at 10:47
2
void f(int *app[][20])

should be

void f(int app[][20])

and the call should be like

f(app);

The changes done in the function f() is visible in main() You can access the array in function f() like app[i][j]

Gopi
  • 19,784
  • 4
  • 24
  • 36
0

the following code correctly implements the definition and passing of a pointer to the 'app[][]' array to function 'f'

Notice the replacement of the 'magic' numbers with #define'd values

Notice the proper use of prototypes.
This will become very important as the projects become larger and consist of multiple files. Although when the project becomes multiple files, then each source file should have its' own header file that contains the prototypes for that source file

The code cleanly compiles and runs correctly

#include <stdio.h>

#define ROWS (20)
#define COLS (20)

void f(int app[][COLS]) ;


int main( void ) 
{
    int app[ROWS][COLS];
    int i, j;

    f(app);

    for (i =0; i< ROWS; i++)
    {
        for (j=0; j<COLS; j++)
        {
            printf("i %d, j%d  val= %d\n", i, j, app[i][j]);
        }
    }

  return 0;
}

void f(int app[][COLS]) 
{
    int i, j;
    for (i =0; i< ROWS; i++)
    {
        for (j=0; j<COLS; j++)
        {
            app[i][j] = i;
        }
    }
}
user3629249
  • 16,402
  • 1
  • 16
  • 17