Thread: void pointer

  1. #1

    void pointer

    I made a function that has a void pointer as a parameter. I don't think I did it right (I've never made a function with a void pointer before). It always returns 0, no matter what. Here is my code:

    Code:
    unsigned int BTFImage::GetData(unsigned char SetFlag, void* dest)
    {
    	switch (SetFlag)
    	{
    	case BTF_WIDTH:
    		dest = &width;
    		return height;
    	default:
    		return 0;
    	}
    }
    PS: There are more cases in the actual function, I just copied and pasted one.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > dest = &width;
    Doesn't work for the same reason SetFlag = 1 wouldn't change the value in the caller.

    Either pass a void** parameter and do
    *dest = &width;

    Or pass a void* parameter as a reference (since this is C++)

    I hope width isn't a private class member, that would be very bad C++
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    You could also do:

    Code:
    	case BTF_WIDTH:
    		*(int *)dest = width;
    		return height;
    	default:
    		return 0;
    This, however, places the burden on the caller to allocate space for the data.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. saying hello and 1st question
    By darksys in forum C Programming
    Replies: 12
    Last Post: 10-31-2008, 02:58 PM
  2. How to better manage large .cpp files
    By 39ster in forum C++ Programming
    Replies: 6
    Last Post: 08-25-2008, 08:24 AM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM