c++ - How to pass a 2d array through pointer in c -


possible duplicate:
passing pointer representing 2d array function in c++

i trying pass 2-dimensional array function through pointer , want modify values.

#include <stdio.h>  void func(int **ptr);  int main() {     int array[2][2] = {         {2, 5}, {3, 6}     };      func(array);      printf("%d", array[0][0]);     getch(); }  void func(int **ptr) {     int i, j;     (i = 0; < 2; i++) {         (j = 0; j < 2; j++) {             ptr[i][j] = 8;         }     } } 

but program crashes this. did wrong?

it crashes because array isn't pointer pointer, try reading array values if they're pointers, array contains data without pointer.
array adjacent in memory, accept single pointer , cast when calling function:

func((int*)array); 

...

void func(int *ptr) {     int i, j;     (i = 0; < 2; i++) {         (j = 0; j < 2; j++) {             ptr[i+j*2]=8;         }     } } 

Comments

Popular posts from this blog

angularjs - Redirect to a new template in angular js -

Oracle SQL. How to select specific IDs where value of columns specify criteria? -

Desktop Launcher for Python Script Starts Program in Wrong Path (Linux) -