typedef Vs #define

Discussion in 'C' started by kanaks_go4ex, Aug 29, 2008.

  1. kanaks_go4ex

    kanaks_go4ex New Member

    Joined:
    Jun 11, 2008
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    Hello guys,

    What is the difference between typedef and #define?

    thanks
    -kanaks
     
  2. oogabooga

    oogabooga New Member

    Joined:
    Jan 9, 2008
    Messages:
    115
    Likes Received:
    11
    Trophy Points:
    0
    There are two differences between define and typedef.

    Firstly, typedef obeys scoping rules just like variables, whereas define
    stays valid until the end of the file (or until a matching undef).

    Secondly, some things can be done with typedef that cannot be done with define.
    Examples:
    Code:
    typedef int* int_p1;
    int_p1 a, b, c;  // a, b, and c are all int pointers.
    
    #define int_p2 int*
    int_p2 a, b, c;  // only the first is a pointer!
    
    Code:
    typedef int a10[10];
    a10 a, b, c; // create three 10-int arrays
    
    Code:
    typedef int (*func_p) (int);
    func_p fp // func_p is a pointer to a function that
              // takes an int and returns an int
    
     
  3. kanaks_go4ex

    kanaks_go4ex New Member

    Joined:
    Jun 11, 2008
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    cool..!
    thanks.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice