Thread: Can anyone explain this?

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    2

    Can anyone explain this?

    Hi,

    My background is mainly assembler and I am working my way through learning C. I was wondering if anyone can tell me what this line of code means:

    Code:
    cmd_def_ptr->handler(argc, arv);
    If someone can explain the logic behind this. I think that cmd_def_prt is a pointer to a structure, but the member seems to be a function, is this correct. Does this effectivley mean:

    "The function 'handler' which is a member of the structure pointed to by cmd_def_ptr"

    The compiler is gcc targetting an embedded processor (Cypress CY16)


    Thanks

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The handler member is probably a function pointer. Functions have address' just like other "normal" variables do. You can assign the address of a function to a function pointer variable and call the function using the function pointer. Later, if necessary, you can reassign the pointer to point to a different function and make the same call again with different results.

    Code:
    void func1(void)
    {
        printf("This is func1.\n");
    }
    
    void func2(void)
    {
        printf("This is func2.\n");
    }
    
    ...
    
    void (*funcptr)(void);  // Declares a function pointer called funcptr that can be used with functions
                            // returning void and taking no parameters
    funcptr = func1;        // Assign the address of func1 to funcptr
    funcptr();              // Effectively calls func1
    funcptr = func2;        // Assign the address of func2 to funcptr
    funcptr();              // Effectively calls func2
    The return type and types and numbers of arguments of the function pointer must match the definition of the function it points to. If you have a function that returns a double and accepts a float and a char* as an argument, the function pointer that is assigned the address of that function must be declared as:
    Code:
    double (*funcptr)(float,char*);
    And you'd call it like so:
    Code:
    double value;
    
    funcptr = somefunc;
    value = funcptr(5.7f,"Hello World");
    Last edited by hk_mp5kpdw; 10-30-2007 at 07:09 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    2
    Thanks!

    I see it now and it makes sense to me now I see your example.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someone explain to me what this code means
    By Shadow12345 in forum C++ Programming
    Replies: 3
    Last Post: 12-22-2002, 12:36 PM
  2. Replies: 4
    Last Post: 11-19-2002, 09:18 PM
  3. explain this loop statement?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-05-2002, 02:46 AM
  4. Pls explain how this program works...
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 01-05-2002, 09:53 AM
  5. Can someone explain "extern" to me?
    By valar_king in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2001, 12:22 AM