C struct and malloc problem (C)

It is amazing how even the smallest program can cause so many problems 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 help?

+4
source share
8 answers

You use two variables named tree and node , but you also have typedef structures ed like tree and node .

Change the 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 return pointer, it 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; } 
+13
source

tree and node β€” your case β€” are type names and should not be used subsequently as variable names.

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

Change (tree*) and (node*) to (struct tree*) and (struct node*) . You cannot just say tree , because it is also a variable.

+3
source

Modify the initTree body as follows:

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

Do not use typedef 'ed names as variable names, and there is no need to throw 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; } 
+1
source

Secondly, Mehrdad’s explanation is a point.

It is not uncommon that in C code, you define a variable with the same name as the name of the structure, for example, "node node;". Perhaps this is not a good style; It is common in, for example, the linux kernel, code.

The real problem in the source code is that the compiler does not know how to interpret the "tree" in "(tree *) malloc". According to a compilation error, it is explicitly interpreted as a variable.

0
source

Besides the original question, this code, even in the correct form, will not work, simply because tree::nodes (sorry for the C ++ notation) as a pointer to a pointer will not point to anything useful after a tree as determined. So tree->nodes[0] , which in the case of ordinary pointers is essentially the same as *(tree->nodes) , cannot be dereferenced. In any case, this is a very strange head for a tree, but you should at least select one node* to initialize this pointer to a pointer:

 tree *initTree() { /* in C code (not C++), don't have to cast malloc return pointer, it implicitly converted from void* */ tree* atree = malloc(sizeof(struct tree)); /* different names for variables */ /* ... */ /* allocate space for numNodes node*, yet numNodes needs to be set to something beforehand */ atree->nodes = malloc(sizeof(struct node*) * atree->numNodes); node* anode = malloc(sizeof(struct node)); atree->nodes[0] = anode; return atree; } 
0
source

Interestingly, it just compiles if you just write distributions as:

  tree * tree = malloc (sizeof * tree);

It is often considered that it is better to use "sizeof variable" rather than "sizeof (type)", in which case the stylistic convention resolves the syntax error. Personally, I think this example is a good example, demonstrating why it is usually a bad idea, since the code is much less confusing if written:

  struct tree * tree = malloc (sizeof * tree);
0
source

Source: https://habr.com/ru/post/1336594/


All Articles