struct qustion..

This is a discussion on struct qustion.. within the C Programming forums, part of the General Programming Boards category; i wrote a simple struct it says i got syntax error i cant se my error here ?? Code: typedef ...

  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,047
    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,185
    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
    21,771
    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;
    };
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 08: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, 02:58 PM

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