Passing void pointer by reference

This is a discussion on Passing void pointer by reference within the C Programming forums, part of the General Programming Boards category; Hi, In the following code, I am passing a void pointer by reference assigning a value to it. But the ...

  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,467
    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
    21,644
    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.
    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
    - - - - - - - - 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, 05: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

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