Thread: Passing pointers by reference

  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
    28,412
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 02:25 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM