5
#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]) {
2
  • why are you trying to run code that does not cleanly compile? Commented Apr 30, 2015 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! Commented May 2, 2015 at 7:56

3 Answers 3

7
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);
Sign up to request clarification or add additional context in comments.

4 Comments

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?
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
Why can't we pass by reference here?
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)
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]

Comments

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;
        }
    }
}

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.