Thread: typedef

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    40

    typedef

    typdef int arr5[5];

    typedef arr5 * p_arr5;

    typedef p_arr5 arrp10[10];

    arr5 togs;

    p_arr5 p2;

    arrp10 ap;

    is this all typedef is valid? I am confuse on how to read this typedef declarations...please give me some technique how to read this kind of declarations...thanks

  2. #2
    Registered User
    Join Date
    Dec 2009
    Location
    /dev/pts/0
    Posts
    29
    Think of a typedef as something that redefines the value of one thing as another. So, for example:

    Code:
    typedef int VARIABLE_TYPE;
    VARIABLE_TYPE this_is_actually_an_int;
    
    typedef std::string STRING_TYPE;
    STRING_TYPE this_is_actually_a_string;
    
    typedef int[50] INTEGER_ARRAY;
    INTEGER_ARRAY this_is_actually_an_array_of_integers;
    (Note that I don't meet the C99 standard in here, which specifies only 32 characters to define a variable as unique . . .)

    You may also think of typedefs as a search-and-replace of sorts. It replaces the second argument with the first.
    Last edited by strange; 12-10-2009 at 12:14 AM.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I'd say, as an alias for a pre-existing (or forward declared) type. Not value, it really has nothing to do with values.

    Just don't typedef pointers, and all is good

  4. #4
    Registered User
    Join Date
    Dec 2009
    Location
    /dev/pts/0
    Posts
    29
    Quote Originally Posted by zacs7 View Post
    Just don't typedef pointers, and all is good
    Aah, words of wisdom. Now, can you please tell that to the developers of SDLNet? . . .

    Actually, typedef'ing pointers isn't too bad (IMO), as long as you don't over-do it. And as long as you give the typedef a good, pointer-sounding name . . .

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Function pointers maybe... but otherwise it's horrible IMO

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Typedefs are read just like variable definitions, except that they start with the word typedef, and what would be the variable name is instead taken to be the alias.

    Usually that means that the type comes right after typedef and before the alias, but arrays and function pointers and anything derived from those two will surround the alias.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Nov 2009
    Location
    wuhan ,hubei ,china
    Posts
    7
    It is not use often ,but know the real meaning is helpful to understand the 'typedef'

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, 08: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