pointers - Passing multidimensional array by reference in C -


#include <stdio.h>  void f(int *app[][20]) {     int i, j;     (i =0; i< 20; i++){         (j=0; j<20; j++){             *app[i][j] = i;         }     } }  int main() {   int app[20][20];   int i, j;   f(&app);     (i =0; i< 20; i++){         (j=0; j<20; j++){             printf("i %d, j%d  val= %d\n", i, j, app[i][j]);         }     }    return 0; } 

what doing wrong here? don't error, there segmentation fault , don't know why.

te.c:15:5: warning: passing argument 1 of ‘f’ incompatible pointer type    f(&app);      ^ te.c:3:6: note: expected ‘int * (*)[20]’ argument of type ‘int (*)[20][20]’  void f(int *app[][20]) { 

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

should be

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

or

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

*app[i][j] = i; 

should be

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

f(&app); 

should be

f(app); 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -