C struct and malloc problem (C)

17,593

Solution 1

You're using two variables named tree and node, but you also have structs typedefed as tree and node.

Change your variable names:

#include <stdio.h>
#include <stdlib.h>

typedef struct node {
    int value;
    struct node *leftChild;
    struct node *rightChild;
} node;

typedef struct tree {
    int numNodes;
    struct node** nodes;
} tree;

tree *initTree() {
   /* in C code (not C++), don't have to cast malloc's return pointer, it's implicitly converted from void* */
   tree* atree = malloc(sizeof(tree)); /* different names for variables */
   node* anode = malloc(sizeof(node));
   atree->nodes[0] = anode;
   return atree;
}

int main() {
    return 0;
}

Solution 2

tree and node is your case are type names and should not be used as variable names later on.

tree *initTree() {
    tree *myTree = (tree*) malloc(sizeof(tree));
    node *myNode = (node*) malloc(sizeof(node));
    myTree->nodes[0] = myNode;
    return myTree;
}

Solution 3

Change (tree*) and (node*) to (struct tree*) and (struct node*). You can't just say tree because that's also a variable.

Solution 4

Change the body of initTree as follows:

tree* myTree = (tree *)malloc(sizeof(tree));
node *myNode = (node *)malloc(sizeof(node));
myTree->nodes[0] = myNode;
return myTree;

Solution 5

Don't use typedef'ed names as variable names, and there is not need to cast malloc(); in C.

#include <stdio.h> 
#include <stdlib.h> 

typedef struct node {
    int value;
    struct node *leftChild;
    struct node *rightChild;
} node;

typedef struct tree {
    int numNodes;
    struct node** nodes;
} tree;

tree *initTree() {
    tree->nodes[0] = malloc(sizeof(node));
    return malloc(sizeof(tree));
}

int main() {
    return 0;
}
Share:
17,593
Gal
Author by

Gal

Updated on June 08, 2022

Comments

  • Gal
    Gal almost 2 years

    It's amazing how even the littlest program can cause so much trouble in C.

    #include <stdio.h> 
    #include <stdlib.h> 
    
    typedef struct node {
        int value;
        struct node *leftChild;
        struct node *rightChild;
    } node;
    
    typedef struct tree {
        int numNodes;
        struct node** nodes;
    } tree;
    
    tree *initTree() {
        tree* tree = (tree*) malloc(sizeof(tree));
        node *node = (node*) malloc(sizeof(node));
        tree->nodes[0] = node;
        return tree;
    }
    
    int main() {
        return 0;
    }
    

    The compiler says:

    main.c: In function 'initTree':
    main.c:17: error: expected expression before ')' token 
    main.c:18: error: expected expression before ')' token
    

    Can you please help?

  • Admin
    Admin over 13 years
    Not necessary (typedef struct tree {} tree).
  • Admin
    Admin over 13 years
    I tried it now, and it seems to compile. However, it works without struct (but only when the variable has a name not clashing with the type name).
  • user541686
    user541686 over 13 years
    @delnan: Exactly my point... hence why he needs to say struct if he doesn't change the rest of his code.
  • Admin
    Admin over 13 years
    If he doesn't change the rest of the code, yes. But the majority (look at the votes) seems to think that renaming the variables is the real solution (it also allows dropping the arguably unnecessary struct before the typename).
  • user541686
    user541686 over 13 years
    @delnan: I certainly agree that that's a better solution, but no one said all answers to the same question have to give the same solution. :)
  • Admin
    Admin over 13 years
    But if there is a better solution, why not propose the better solution (and perhaps mention inferior ones)?
  • user541686
    user541686 over 13 years
    @delnan: This is just the first fix that jumped to my mind, and it's a bit pointless to change anything now.
  • Gal
    Gal over 13 years
    So should I never try to cast a void returned from malloc?
  • wkl
    wkl over 13 years
    @sombe - you don't have to (in C), the conversion from void * to your pointer type is implicit.
  • Jens Gustedt
    Jens Gustedt over 13 years
    @sombe, yes. If you do, you might hide some implicit casts to int if the prototype of malloc is not included.