1
typedef struct tree_node
{
    int data;
    struct tree_node *left;
    struct tree_node *right;
}node;

node* newnode(int data)
{
    node *node=(node*)(malloc(sizeof(struct tree_node)));
    node->data=data;
    node->left=NULL;
    node->right=NULL;

    return(node);
}

The code is showing error in this part. What is the error? Why is it not compiling? EDIT: The error shown is error: expected primary-expression before ')' token. However if I change the code to

node* newnode(int data)
{
    node *node1=(node*)(malloc(sizeof(struct tree_node)));
    node1->data=data;
    node1->left=NULL;
    node1->right=NULL;

    return(node1);
}

it works perfectly. What is the reason for this?

cst
  • 43
  • 6
  • what error? Please give us more information. – Logicrat Aug 12 '14 at 10:56
  • Could you tell us what error you're getting and where? – flau Aug 12 '14 at 10:56
  • I think `node *node=(node*)(malloc(sizeof(struct tree_node)));` causing the error! change `*node` to `*node1`- `node *node1=(node*)(malloc(sizeof(struct tree_node)));` – Sathish Aug 12 '14 at 10:56
  • Please don't tag questions as both C and C++ unless your question is actually about both C and C++. Which is it? It looks like you've written it to be used by a C++ compiler. Is that correct? If so, please remove the C tag. If not, please remove the C++ tag. Either language has correct answers that don't apply to the other language. –  Aug 12 '14 at 11:15
  • Sorry..noob user.. I edited the question – cst Aug 12 '14 at 11:20

1 Answers1

3

You use node as a typename, but it's a variable. Change it to:

typedef struct tree_node
{
    int data;
    struct tree_node *left;
    struct tree_node *right;
};

tree_node* newnode(int data)
{
    tree_node *node=(tree_node*)(malloc(sizeof(struct tree_node)));
    node->data=data;
    node->left=NULL;
    node->right=NULL;

    return(node);
}

and next time please share the compiler error too, don't expect us to find it out...

glezmen
  • 554
  • 2
  • 5
  • `typedef struct tree_node {` .. `};` is invalid . Some common compilers accept it and appear to silently ignore the `typedef` keyword. – M.M Aug 12 '14 at 11:22
  • also, [don't cast malloc](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – M.M Aug 12 '14 at 11:23
  • 1
    @MattMcNabb That's actually valid IIRC, surprisingly enough, simply because the standard (possibly accidentally) fails to prohibit it. But the `typedef` keyword is indeed useless there. And since the question is now properly tagged as C, `(tree_node*)` needs to be `(struct tree_node*)`, or `typedef struct tree_node { ... } tree_node;` is required. –  Aug 12 '14 at 11:27
  • @hvd hmm, C11 6.7.8/3 seems to imply that there must be an identifier but it is not very clear. Is there another SO question on this topic already? – M.M Aug 12 '14 at 11:32
  • @MattMcNabb I'm assuming 6.7.8 is Type definitions? (That's 6.7.7 in C99.) It doesn't prohibit this; it states the behaviour for each declarator, and doesn't treat "no declarators" as a special case, so specifies that `typedef` be ignored. The constraint in 6.7/2 ("A declaration shall declare at least a declarator (other than the parameters of a function or the members of a structure or union), a tag, or the members of an enumeration.") is what prohibits `typedef int;`, but in this case, there is a tag, so the constraint is not violated. I have no link to other questions on this, sorry. –  Aug 12 '14 at 11:37