Typedef

This is a discussion on Typedef within the C Programming forums, part of the General Programming Boards category; what is this Code: typedef int (*pfunc)(void); pfunc Foo; What is Foo here... i have used typedef but not like ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    11

    Typedef

    what is this

    Code:
    typedef int (*pfunc)(void);
    pfunc Foo;
    What is Foo here...
    i have used typedef but not like this..

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >What is Foo here...
    A pointer to a function that takes no arguments and returns a value of type int.

    >i have used typedef but not like this..
    Then follow the same rules you use to figure out any typedef:
    Code:
    typedef int foo_t;
    A typedef uses the same syntax as a variable declaration, except instead of creating a variable with the specified name, it creates a type synonym with the specified name. So in the above typedef, foo_t is a synonym for int. Now let's declare a pointer to a function:
    Code:
    #include <stdio.h>
    
    int do_something(void)
    {
      puts("foo");
    }
    
    int main()
    {
      int (*foo)(void);
    
      foo = &do_something;
      foo();
    
      return 0;
    }
    That's tedious, so instead of declaring the pointer type directly, make it a typedef:
    Code:
    #include <stdio.h>
    
    typedef int (*foo_t)(void);
    
    int do_something(void)
    {
      puts("foo");
    }
    
    int main()
    {
      foo_t foo;
    
      foo = &do_something;
      foo();
    
      return 0;
    }
    Problem solved as long as you recognize the type being typedef'd.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    Quote Originally Posted by vijay s View Post
    what is this

    Code:
    typedef int (*pfunc)(void);
    pfunc Foo;


    i have used typedef but not like this..
    If you've got a unix OS, try using cdecl.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  3. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 09:22 AM
  4. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 07:35 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM

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