void pointer

This is a discussion on void pointer within the C++ Programming forums, part of the General Programming Boards category; I made a function that has a void pointer as a parameter. I don't think I did it right (I've ...

  1. #1
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552

    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
    32,852
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    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, 03:58 PM
  2. How to better manage large .cpp files
    By 39ster in forum C++ Programming
    Replies: 6
    Last Post: 08-25-2008, 09:24 AM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  5. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 06:20 AM

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