Passing pointers by reference

This is a discussion on Passing pointers by reference within the C Programming forums, part of the General Programming Boards category; Just an apparently simple question. How to pass pointers by reference in C? What I mean is how to get ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    52

    Question Passing pointers by reference

    Just an apparently simple question. How to pass pointers by reference in C?
    What I mean is how to get a memory address to be modified inside a function or procedure and to be able to pass this modified address to another function, for example to the main.

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by JOCAAN View Post
    Just an apparently simple question. How to pass pointers by reference in C?
    What I mean is how to get a memory address to be modified inside a function or procedure and to be able to pass this modified address to another function, for example to the main.
    You can use a pointer of a pointer.
    Code:
    int main () {
        int * a = NULL;
        func (&a);
        printf ("%d", *a);
        return 0;
    }
    
    void func (int ** x) {
       *x = malloc (sizeof (int));
       **x = 5;
    }
    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by JOCAAN
    How to pass pointers by reference in C?
    You pass a pointer to a pointer.

    EDIT:
    Note that QuantumPete's example is incomplete; it is, after all, merely an example.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    You mean pointers to pointers? That's easy:

    Code:
    int foo(int **bar)
    {
            return bar(bar);
    }
    
    ...
    
    int main(int argc, char **argv)
    {
            int *barpointer = &foo
            ...
            **barpointer = foo(&barpointer);
            ...
    }
    or you can use int **pointer = &anotherpointer; etc...

  5. #5
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    You shouldn't use foo both for a variable (undeclared I might add) and a function. It might be confusing for the OP.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by QuantumPete View Post
    You shouldn't use foo both for a variable (undeclared I might add) and a function. It might be confusing for the OP.

    QuantumPete
    And to me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing by Reference. Simple question.
    By d3m105 in forum C Programming
    Replies: 6
    Last Post: 10-31-2007, 12:47 PM
  2. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  3. Passing an array as reference to a function?
    By Kylecito in forum C++ Programming
    Replies: 10
    Last Post: 03-11-2006, 01:25 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21