0

So I have a recursive function that requires it to be of type void, without turning to global variables how can I pass a 2d array as a pointer and dereference once I edit values in the recursive function? I already tried int * m[10][10], I can't show all of the code since it is an assignment, pointers aren't required in the assignment but global variables are not permitted

Void h(int * m[10][10]){
    int x = 5;
    int y = 5;
    *m[x][y]=7;
}

That's the general idea

ultrainstinct
  • 255
  • 4
  • 15
  • `int **` is equivalent to int[][], you may have to change the signature to something like `void h(int **array2d, int rows, int columns)` – ichramm Jan 23 '14 at 20:23
  • 5
    @ichramm: `int **` is not equivalent to `int [][]`. The latter is not valid C syntax, but, if necessary dimensions are supplied, it is either a pointer to an array of `int` (in a function parameter) or an array of array of `int` (otherwise). Neither of these is a pointer to a pointer to `int`. – Eric Postpischil Jan 23 '14 at 20:24
  • Would you dereference the same way. – ultrainstinct Jan 23 '14 at 20:25
  • @EricPostpischil You're right, I was just trying to make a point, hope the later method signature clarifies better. It's been a long time since the last time I wrote pure C code http://c-faq.com/aryptr/pass2dary.html – ichramm Jan 23 '14 at 20:26
  • 2
    @ichramm: The latter signature has the same problem. If a two-dimensional array is defined with `int m[10][10]`, it cannot be passed to a parameter declared as `int **array2d`, even if the numbers of rows and columns are also passed. There is no conversion from “array of array of `int`” to “pointer to pointer to `int`”. The latter requires that an array of pointers exist, and no such objects exist just for an array of array of `int`. – Eric Postpischil Jan 23 '14 at 20:29
  • See I am trying to recurse through neighbours of m so I don't know if any of these would work – ultrainstinct Jan 23 '14 at 20:31
  • @user1995241: What is a neighbor of m? – Eric Postpischil Jan 23 '14 at 20:33
  • By neighbour I worded it wrong but I mean m[x+1][y] m[x-1][y] and etc – ultrainstinct Jan 23 '14 at 20:37
  • @user1995241: So you do not want to pass the array `m` every time, you want to pass a pointer into some particular position in `m`? That is a different question from passing an array. – Eric Postpischil Jan 23 '14 at 21:17
  • I meant pass an 2d pointer array and dereferencd when you need to edit it, this is getting more complicated than I had hoped – ultrainstinct Jan 23 '14 at 21:25

3 Answers3

1

Simply try this

Void h(int  m[][10]) {
    ...

    m[x][y]=7;
}  

Call your function either as

h(&m[0]);   // Passing the address of first row of 2D array m.  

or

h(m);       // As array names decays to pointer to first element of the array. Note that 
            // m will decays to pointer to m[0], not m[0][0].
haccks
  • 104,019
  • 25
  • 176
  • 264
1

It all depends on how you declared the array supposed to be passed to your function.

If you declared it like this

int my_array[10][10];     // array declaration

then

void fun (int m[10][10]); // prototype of a function accepting my_array
fun (my_array);           // calling the function

is what you are looking for.

In that case, m is a constant pointer to 100 contiguous ints that are accessed as a 10x10 2D array.

Other possible variant:

int * my_array[10];
for (i = 0 ; i != 10 ; i++) my_array[i] = malloc (10*sizeof(int));

void fun (int m[10][]); // these syntaxes are equivalent
void fun (int * m[10]);

or this one:

int ** my_array;
my_array = malloc (10 * sizeof (int*));
for (i = 0 ; i != 10 ; i++) my_array[i] = malloc (10*sizeof(int));

void fun (int m[][]); // these syntaxes are equivalent
void fun (int * m[]);
void fun (int **  m);

or this pathological one:

int (* my_array)[10];
int a0[10];
int a1[10];
/* ... */
int a9[10];
my_array = malloc (10 * sizeof (int *));
my_array[0] = a0;
my_array[1] = a1;
/* ... */
my_array[9] = a9;

void fun (int m[][10]); // these syntaxes are equivalent
void fun (int (* m)[10]);

If your variable declaration and function prototype are not consistent, you will not access the array properly from within your function, read incoherent values, and mess up your memory and possibly crash if you attempt to modify an element.

If you have nothing better to do, you can read this little essay of mine on the subject for further details.

Community
  • 1
  • 1
kuroi neko
  • 8,479
  • 1
  • 19
  • 43
-1
void h(int** m) {
    int x = 5;
    int y = 7;
    m[x][y] = 7;
}

int main(int argc, char* argv[])
{
    int** a = (int **)malloc(10*sizeof(int *));

    for (int i=0; i<10; i++) {
        a[i] = (int *)malloc(10*sizeof(int));
    }

    h(a);

    return 0;
}
chessweb
  • 4,613
  • 5
  • 27
  • 32