Thread: struct qustion..

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    struct qustion..

    i wrote a simple struct
    it says i got syntax error
    i cant se my error here
    ??
    Code:
    typedef struct node{
        node *left;
        node *right;
        int data;
    
    }node;

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    do you want the same name for the struct tag and its alias?

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    We've done this before. The name "node" does not exist until you get to the actual typedef at the bottom line. Until then you must use the actual name of the type, "struct node".

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, because node was not declared until after the struct was defined. Here are two solutions:
    Code:
    typedef struct node {
        struct node *left;
        struct node *right;
        int data;
    } node;
    Code:
    typedef struct node node;
    
    struct node {
        node *left;
        node *right;
        int data;
    };
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    this is a combination of struct and typedef
    instead writing struct node each time.

    using this code i can write only "node"
    which i wrote in the end.

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by laserlight View Post
    Yes, because node was not declared until after the struct was defined. Here are two solutions:
    Code:
    typedef struct node {
        struct node *left;
        struct node *right;
        int data;
    } node;
    Code:
    typedef struct node node;
    
    struct node {
        node *left;
        node *right;
        int data;
    };
    ahhhh thanks

  7. #7
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by transgalactic2 View Post
    this is a combination of struct and typedef
    instead writing struct node each time.

    using this code i can write only "node"
    which i wrote in the end.
    I was begining to wonder if u even read the previous post before i got to the bottom.
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  8. #8
    Registered User
    Join Date
    Nov 2008
    Location
    China
    Posts
    17
    That's maybe two direction link list

    This is my first post!

    I‘'m from China and my English is bad……

    Do you understand what I say??
    Last edited by liyanhong; 11-18-2008 at 09:15 AM.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by liyanhong View Post
    That's maybe two direction link list

    This is my first post!

    I‘'m from China and my English is bad……

    Do you understand what I say??
    The structure is a binary tree node and I'm sure that transgalactic2 knows that too, since [s]he is working on a binary tree insertion function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    its HE

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segfault with Linked List Program
    By kbrandt in forum C Programming
    Replies: 1
    Last Post: 06-23-2009, 07:13 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM