Thread: Passing double pointer by reference

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    3

    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?
    Last edited by snowmoo; 10-10-2012 at 03:47 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing void pointer by reference
    By sakura in forum C Programming
    Replies: 3
    Last Post: 08-23-2012, 01:16 AM
  2. Passing structure by reference or pointer?
    By 7force in forum C Programming
    Replies: 8
    Last Post: 12-13-2010, 06:49 PM
  3. Basic Question about passing pointer by reference
    By Vall3y in forum C++ Programming
    Replies: 5
    Last Post: 10-05-2009, 01:44 AM
  4. Passing a pointer as a reference
    By hYph3n in forum C++ Programming
    Replies: 5
    Last Post: 10-04-2006, 01:45 PM
  5. Passing pointer...reference...
    By Cheeze-It in forum C++ Programming
    Replies: 1
    Last Post: 07-21-2002, 07:35 PM

Tags for this Thread