My First Thread (functinal pointers)

This is a discussion on My First Thread (functinal pointers) within the C Programming forums, part of the General Programming Boards category; Hi Guys This my first thread Hope it is in sync with other threads!!! I have been programming in C++ ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    4

    Question My First Thread (functional pointers)

    Hi Guys
    This my first thread
    Hope it is in sync with other threads!!!

    I have been programming in C++ apps for the past 2 years
    but suddenly I feel I should concentrate on C and systems side
    my knowledge on C is moderate and I need help from this forum
    to pick it up

    I have question too!!
    where exactly do we use functional pointers instead of orinary functional calls?

    I have vague knowledge on this and some say you can avoid pitfalls of using switch case statements by using these FP's

    Please help me on this by giving a full example
    bye
    Last edited by Denis Itchy; 08-23-2002 at 01:58 AM.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571

    Re: My First Thread (functional pointers)

    Originally posted by Denis Itchy
    Hi Guys
    This my first thread
    Hope it is in sync with other threads!!!

    I have been programming in C++ apps for the past 2 years
    but suddenly I feel I should concentrate on C and systems side
    my knowledge on C is moderate and I need help from this forum
    to pick it up

    I have question too!!
    where exactly do we use functional pointers instead of orinary functional calls?

    I have vague knowledge on this and some say you can avoid pitfalls of using switch case statements by using these FP's

    Please help me on this by giving a full example
    bye
    Welcome to the boards Okay, function pointers. Well I'll assume you know what they are but don't know when to use them. Just a quick example are callback functions. Callback functions are function pointers!! Also, you could have an array of function pointers for sin, cos and tan if you wanted and easily manipulate the pointer to call the functions. You'll see lots of function pointers in the Win32 API. Any specific questions?
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    Here's something which you might have to do as a big series of if/else or as a case statement, if it were not for function pointers.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define ASIZE(x)    (sizeof(x)/sizeof(x[0]))
    
    /* a typedef to simplify the pointer to a function declaration */
    typedef void (*fnptr)(char *);
    
    /* associate a word with a function */
    typedef struct {
        char    *keyword;
        fnptr    func;
    } table_st;
    
    /* the functions we want to call */
    void do_file ( char *line ) {
        printf( "do_file %s\n", line );
    }
    void do_edit ( char *line ) {
        printf( "do_edit %s\n", line );
    }
    void do_print ( char *line ) {
        printf( "do_print %s\n", line );
    }
    
    /* associate keywords with actions */
    table_st table[] = {
        { "file", do_file },
        { "edit", do_edit },
        { "print", do_print },
    };
    
    /* search the table, and call the associated function */
    void lookup_function ( char *keyword, char *param ) {
        int i;
        for ( i = 0 ; i < ASIZE(table) ; i++ ) {
            if ( strcmp( table[i].keyword, keyword ) == 0 ) {
                table[i].func( param );
            }
        }
    }
    
    int main ( ) {
        lookup_function( "file", "foo.txt" );
        lookup_function( "edit", "bar.txt" );
        return 0;
    }

  4. #4
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212
    Normally, you would use a function pointer when you want to have the same interface, but different functionally when a specific function gets called.

    Plug-Ins are a perfect example. In photoshop, it has a standardized way in which it will pass information and then jump to a plug-in. This doesn't change. But the functionality of the plug-in DOES.

    As such, when each plug-in is loaded, photoshop keeps track of a function pointer for that plug-in. Then when it wants to interract with that plug-in, it can use a well-defined, generic interface, that won't change with each upgrade of photoshop.

    Each plug-in does something different, but photoshop doesn't care, they all look the same to it.

    The power of the function pointer!

    Code:
    typedef int (*FUNC)(int arg1,char arg2);
    
    typedef struct piStruc
       {
       struct piStruc    *next;
       FUNC                 piFunction;
       long                   data1;
       char                   data2;
       unsigned char   *piStorage;
       }pluginRec;
    It is not the spoon that bends, it is you who bends around the spoon.

  5. #5
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    >unsigned char

    What would be the point of declaring a char unsigned? I thought valid values for a char were only 0-255...

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >What would be the point of declaring a char unsigned?
    To be sure that you are getting what you want. Whether a char is signed or unsigned by default is implementation-dependent.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Sebastiani
    Guest

    Thumbs up

    Hey, Salem, great example! What an easy way to match commands with funcs.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    BTW: chars are _always_ unsigned by default.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    > BTW: chars are _always_ unsigned by default
    No they are not
    From the standard
    The three types char, signed char, and unsigned char are collectively called
    the character types. The implementation shall define char to have the same range,
    representation, and behavior as either signed char or unsigned char.

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Ok, but before you throw the book at me, name one platform that uses the signed sort? Hmmm? *taps foot impatiently*
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    > name one platform that uses the signed sort
    Your standard Intel x86 perhaps

    From the VC++ manual page
    /J (Default char Type is unsigned)

    This option changes the default char type from signed char to unsigned char, and the char type is zero-extended when widened to an int type. If a char value is explicitly declared signed, the /J option does not affect it, and the value is sign-extended when widened to an int type.

    The /J option defines _CHAR_UNSIGNED, which is used with #ifndef in the LIMITS.H file to define the range of the default char type.
    From the GCC manual page, which supports a wide variety of processors
    -funsigned-char
    Let the type char be unsigned, like unsigned char.
    Each kind of machine has a default for what char should be. It is either like unsigned char by default or like signed char by default.

    Ideally, a portable program should always use signed char or unsigned char when it depends on the signedness of an object. But many programs have been written to use plain char and expect it to be signed, or expect it to be unsigned, depending on the machines they were written for. This option, and its inverse, let you make such a program work with the opposite default.

  12. #12
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I guess that settles it then
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  13. #13
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212
    What would be the point of declaring a char unsigned?
    It's an old habit. Due to portability concerns, I don't assume the compiler will provide a variable as 'unsigned'. However the standard may be now, there are a good many compilers out there (probably older ones, now) that define the base types as automatically being signed, unless specified otherwise.

    For example--

    short (-32768 to +32768)
    long (-2.1GB to +2.1GB)
    char (-127 to +127)

    All are considered as being able to hold a positive or negative value. That means by default they are signed.

    By using the keyword 'unsigned', I tell the compiler to use the MSB for value, rather than for sign.

    unsigned short (0 to 65535)
    unsigned long (0 to 4.2GB)
    unsigned char (0 to 255).

    Hope that makes sense.
    It is not the spoon that bends, it is you who bends around the spoon.

  14. #14
    Registered User
    Join Date
    Aug 2002
    Posts
    4

    Smile thanks

    Thanks a lot guys for updating me about functional pointers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Massive Confusion with Pointers
    By guitarist809 in forum C++ Programming
    Replies: 8
    Last Post: 07-22-2008, 04:01 PM
  2. user thread library
    By Eran in forum C Programming
    Replies: 4
    Last Post: 06-17-2008, 01:44 AM
  3. Replies: 2
    Last Post: 07-01-2007, 07:11 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Replies: 12
    Last Post: 05-17-2003, 05:58 AM

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