Thread: Passing void pointer by reference

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    24

    Passing void pointer by reference

    Hi,
    In the following code, I am passing a void pointer by reference assigning a value to it. But the value is not reflected in the main program.
    Code:
    #include<stdio.h>
    
    
    void function(void *p)
    {
        int i=9;
        int *a=&i;
        p=(void *)a;
    }
    
    
    int main()
    {
        int *p;
        p=(int *)malloc(sizeof(int));
        function((void *)p);
        printf("%d",*p);
        return 0;
    }
    It's printing a garbage value. Please tell me what is the correct way of passing a void pointer by reference and getting a value in it from the function.
    Thanks in advance!

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You mean you want to access the value pointed to by p? You must make sure the types match. First, no need for all the casting to/from void *. Every pointer type can be cast to/from void *. Also, you must make sure the types of what you assign, etc match. For example, in function, p is pointer to int, so *p is the int it points to. If you want to assign the value of i to the value p points to, just do:
    Code:
    *p = i;
    No need for the a variable.

    Also, you don't necessarily need to malloc memory for this, you can just do:
    Code:
    int x;
    function(&x);  // pass address of (pointer to) x to function()

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sakura
    I am passing a void pointer by reference assigning a value to it.
    No, the pointer is passed by value. The fact that you assign a value to the pointer is a separate matter.

    To pass a pointer by reference, you should pass a pointer to the pointer.
    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
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by sakura View Post
    In the following code, I am passing a void pointer by reference assigning a value to it. But the value is not reflected in the main program.
    Actually, you are passing a void pointer BY VALUE, simulating pass-by-reference semantics.

    You never assign to *p in function, so how do you expect the value to be changed in main? Assigning to p just modifies the local value of the pointer.

    Code:
    #include<stdio.h>
    
    void function(void *p) {
        int *ip = (int*)p;
        *ip = 9;
    }
    
    int main() {
        int p;
        function((void*)&p);
        printf("%d", p);
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

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