Passing double pointer by reference
Hi all
I have a 2D array created by double pointer. I would like to pass my double pointer by reference to a function. However, I tried something like function_name(int **&matrix) but it shows compilation error. So I wonder if there is any alternative way of passing the double pointer by reference?
I also tried to use triple pointer in order to pass the double pointer by reference but it doesn't work.
My code is something like this for the triple pointer.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void print(int** a)
{
int i, j;
for(i = 0; i <2; i++){
for(j=0;j<2;j++)
printf("%d ", a[i][j]);
printf("\n");
}
}
void fill(int*** a)
{
int i, j;
*a = (int**) malloc(sizeof(int*) * 2);
for( i = 0; i < 2; i++)
*a[i] = (int*) malloc(sizeof(int)*2);
for(i = 0; i <2; i++)
for(j=0;j<2;j++)
*a[i][j] = 9;
}
int main(int argc, char *argv[])
{
int **a;
fill(&a);
print(a);
return 0;
}
Anyone can help me?