Thread: struct and define problem

  1. #1
    hurry up
    Guest

    struct and define problem

    what the meaning of the first 2 lines?

    #define NodePtr struct Node*
    typedef NodePtr List;

    struct Node
    {
    ElementType data;
    NodePtr next;
    };

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Using [code] [/code] tags presents code in a more appealing way.
    Code:
    #define NodePtr struct Node*
    typedef NodePtr List;
    
    struct Node
    {
       ElementType data;
       NodePtr next;
    };
    Regarding the #define, after preprocessing, the above code would be as follows.
    Code:
    typedef struct Node* List;
    
    struct Node
    {
    ElementType data;
    struct Node* next;
    };
    The typedef creates a synonym for the type specified. So with the above code, you could later declare something like the following.
    Code:
    {
        struct Node node;
        List list = &node;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 10:56 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM