Thread: Function Pointer

  1. #1
    Registered User
    Join Date
    Jul 2003

    Function Pointer

    I have a function in my code that I want to return a pointer to a function. The result would then invoke the returned function. How would I set this up? Here is the code for the function that returns one of two pointer to another function.
    USHORT * getPixelFormat()
    	DDPIXELFORMAT ddpixel;
    	//clear out structure
    	memset(&ddpixel, 0, sizeof(ddpixel));
    	//set length
    	ddpixel.dwSize = sizeof(ddpixel);
    	//use primary surface interface to get pixel format
    	//determine function corresponding to pixel format
    	BOOL RGB = (ddpixel.dwFlags & DDPF_RGB);
    	// test for mode 5.5.5
    	if (RGB & (ddpixel.dwRGBBitCount==15))    
    		return RGB16BIT555;                   
    	if (RGB & (ddpixel.dwRGBBitCount==16))
    		return RGB16BIT565;
    	return NULL;
    Here is more of the code I have tried.
    USHORT * getPixelFormat();
    USHORT RGB16BIT555(int r, int g, int b);
    USHORT RGB16BIT565(int r, int g, int b);
    USHORT (*pfnColor)(int,int,int);			 // pointer to RGBcolor function
    	// Determine color function based on pixel format
    	pfnColor = getPixelFormat();
    void Plot_Pixel(int x, int y, int red, int green, int blue, USHORT * video_buffer, int lpitch)
    	USHORT pixel = (*pfnColor)(red,green,blue);
    Last edited by curlious; 11-04-2003 at 06:15 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    If you want to keep your sanity, make a typedef for the type of the function you want to return a pointer to.

    After that, its dead easy
    #include <stdio.h>
    typedef void (*fp)(void);
    void foo ( void ) {
        printf( "This is foo\n" );
    void bar ( void ) {
        printf( "This is bar\n" );
    fp chooser ( int which ) {
        if ( which ) {
            return foo;
        } else {
            return bar;
    int main ( void ) {
        fp one = chooser(0);  // grab a function
        fp two = chooser(1);
        one();  // and call it
        // combined
        return 0;
    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
    Jul 2003
    Thanks Salem, my program compiles now but doesn't work properly.

    Basicly I was trying to implement the proper pixel format instead of just assuming 565. When I was making the assumption and using a #define for my RGB value it was working now it will not plot and I am not sure why. I will repost the entire code under the windows section and maybe someone can help me figure out whats going wrong.

    Ignore the above, doh I was using the & operator in place of the logical && in two of my tests. Again thanks for the help on the pointer to function aspect
    Last edited by curlious; 11-04-2003 at 07:08 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Function Pointer help
    By Skydt in forum C Programming
    Replies: 5
    Last Post: 12-02-2005, 09:13 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM