"void*" parameters

This is a discussion on "void*" parameters within the C Programming forums, part of the General Programming Boards category; Does anyone have an idea of how to write functions that accept a void pointer? eg. fread and fwrite both ...

  1. #1
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788

    "void*" parameters

    Does anyone have an idea of how to write functions that accept a
    void pointer?

    eg. fread and fwrite both accept "void* buffer"

    how does the compiler know how to cast it, or is this only done
    in assembly?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's of type 'void'. It can be dereferenced as whatever you want. The compiler simply looks at what you're making an assignment to, and it dereferences it as such. In C++ you have to manually type cast it.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    #include <stdio.h>
    
    void myfunc(void *vp)
    {
    	int *ip = vp;
    	printf ("The int value is %d\n", *ip);
    }
    int main(void)
    {
    	int i = 10;
    	
    	myfunc(&i);
    	return 0;
    }
    In this example, the function myfunc takes the void pointer and assigns it to an int pointer. You can then use ip in the normal manner.

    This particular example is a bit lame, but it gives you the idea.

    One more thing, you must assign the void pointer to a known type in order to do arithmatic with it. See http://www.eskimo.com/~scs/C-faq/q11.24.html
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    thanx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 07-21-2008, 07:04 AM
  2. function with variable number of parameters
    By mikahell in forum C++ Programming
    Replies: 3
    Last Post: 07-23-2006, 04:35 PM
  3. Additional parameters for operator delete
    By darksaidin in forum C++ Programming
    Replies: 0
    Last Post: 09-21-2003, 12:46 PM
  4. Passing parameters from VB to C++ through ActiveX DLL
    By torbjorn in forum Windows Programming
    Replies: 0
    Last Post: 12-10-2002, 03:13 AM
  5. command-line parameters.
    By Tombear in forum C Programming
    Replies: 2
    Last Post: 10-28-2001, 08:40 AM

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