# Binary Tree/Struct Problem

Page 1 of 1

## 3 Replies - 995 Views - Last Post: 09 November 2012 - 08:23 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=299417&amp;s=3f15b9b4f5261fc6c139fe5c50d5db3e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 crapmyster

Reputation: 0
• Posts: 78
• Joined: 12-October 11

# Binary Tree/Struct Problem

Posted 09 November 2012 - 07:58 AM

Hello,

I'm just starting to get a binary tree on the go. I've got a problem already which I can't get my head round. I swear that I'm doing this correctly but not according to the compiler. I'm getting the error 'unknown type name 'node''./

```#include <stdio.h>
#include <stdlib.h>
struct Node
{
int input;
Node * left
Node * right;
};

```

Is This A Good Question/Topic? 0

## Replies To: Binary Tree/Struct Problem

### #2 sepp2k

• D.I.C Lover

Reputation: 2549
• Posts: 4,069
• Joined: 21-June 11

## Re: Binary Tree/Struct Problem

Posted 09 November 2012 - 08:15 AM

In C (unlike C++) the name of a struct is not a valid type name by itself - it has to be prefaced with struct to become a type name.

So to declare a variable containing a Node pointer, write struct Node* varname;.

### #3 ivaylo9603

• New D.I.C Head

Reputation: 1
• Posts: 21
• Joined: 05-November 12

## Re: Binary Tree/Struct Problem

Posted 09 November 2012 - 08:19 AM

There is a syntax error in the posted code. It should either have a semi-colon after the declaration of pointer left, or both pointers- left and right should both be declared after the datatype in a single line, separated with commas as follows:
```Node *left, *right;
//or
Node* left;
Node* right;

```

Sent from my GT-P3110

This post has been edited by ivaylo9603: 09 November 2012 - 08:23 AM

### #4 baavgai

• Dreaming Coder

Reputation: 7113
• Posts: 14,830
• Joined: 16-October 07

## Re: Binary Tree/Struct Problem

Posted 09 November 2012 - 08:23 AM

In C, use typedef to make life easier.

e.g.
```typedef struct NodeStruct {
int input;
struct NodeStruct *left, *right;
} Node;

typedef struct {
} List;

```

Note, you must use the full struct declaration in the node struct, since you haven't hit the typedef yet. After that, you can just use the typedef'd name.

Also, for linked lists, make your life easier and use that struct for it. Passing pointers to pointers or passing back head pointers is unnecessary confusion.

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }