weird typedef syntax

This is a discussion on weird typedef syntax within the C Programming forums, part of the General Programming Boards category; Heya, so I can't wrap my mind a typedef'ed function pointer in a header I want to understand. It's of ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    8

    weird typedef syntax

    Heya, so I can't wrap my mind a typedef'ed function pointer in a header I want to understand. It's of the form:

    typedef void (*T_FUNC_CALLBACK) (UINT8 *setupPtr, UINT8 *dataPtr);

    Tell me if I'm wrong: the type is void, and the name is T_FUNC_CALLBACK. But how can a function pointer be a void type? Am I missing something?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The type is "pointer to function returning void taking two arguments, each of type UINT8*".

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    8
    I'm not too familiar with function ptr syntax, so I'm trying to understand: what does this typedef do for the code? Without it, I can already use *T_FUNC_CALLBACK. Since it has the same name, and same function, the typedef seems extraneous code.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by ngloosh View Post
    I'm not too familiar with function ptr syntax, so I'm trying to understand: what does this typedef do for the code? Without it, I can already use *T_FUNC_CALLBACK. Since it has the same name, and same function, the typedef seems extraneous code.
    Extraneous? You'd rather type:

    Code:
    void (*) (UINT8 *setupPtr, UINT8 *dataPtr)
    Everywhere instead of:

    Code:
    T_FUNC_CALLBACK
    ? Weird.

    EDIT: And your statement that "You can already use T_FUNC_CALLBACK" is untrue, since this typedef is what defines that name.
    Last edited by brewbuck; 06-05-2008 at 10:31 AM.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by ngloosh View Post
    I'm not too familiar with function ptr syntax, so I'm trying to understand: what does this typedef do for the code? Without it, I can already use *T_FUNC_CALLBACK. Since it has the same name, and same function, the typedef seems extraneous code.
    You can't use T_FUNC_CALLBACK without the typedef. What the typedef does is make the declaration
    Code:
    T_FUNC_CALLBACK some_function
    equivalent to
    Code:
    void (*some_function)(UINT8 *setupPtr, UINT8 *dataPtr)
    If you ever try to do *T_FUNC_CALLBACK in your code, it won't compile, in much the same way that you can't use *char to dereference a string.

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    8
    cool, cool. Both of you make sense. Thanks.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,593
    A better way to do it IMO, is instead of:
    Code:
    typedef void (*T_FUNC_CALLBACK) (UINT8 *setupPtr, UINT8 *dataPtr);
    T_FUNC_CALLBACK ptr = &something;
    ptr();
    ...do...
    Code:
    typedef void (T_FUNC_CALLBACK) (UINT8 *setupPtr, UINT8 *dataPtr);
    T_FUNC_CALLBACK* ptr = &something;
    ptr();
    It makes it more obvious it's a pointer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM
  4. Zipping files
    By CompiledMonkey in forum C Programming
    Replies: 19
    Last Post: 03-06-2003, 11:23 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

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