Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two or more data types in declaration specifiers error [closed]

Tags:

c

I am pretty new to C.

I am getting this error:

incompatible implicit declaration of built-in function ‘malloc’

Even when I fix the code based on the answers to include <stdlib.h>, I still get:

two or more data types in declaration specifiers

When trying to do this:

struct tnode {     int data;     struct tnode * left;     struct tnode * right; }  struct tnode * talloc(int data){     struct tnode * newTnode;     newTnode = (struct tnode *) malloc (sizeof(struct tnode));     newTnode->data = data;     newTnode->left = NULL;     newTnode->right = NULL;     return newTnode; } 

How do I fix it?

like image 238
SuperString Avatar asked Jan 20 '10 04:01

SuperString


2 Answers

You have to put ; behind the struct declaration:

struct tnode {     int data;      struct tnode * left;     struct tnode * right; }; // <-- here 
like image 149
sth Avatar answered Oct 04 '22 23:10

sth


Your original error was because you were attempting to use malloc without including stdlib.h.

Your new error (which really should have been a separate question since you've now invalidated all the other answers to date) is because you're missing a semicolon character at the end of the struct definition.

This code compiles fine (albeit without a main):

#include <stdlib.h>  struct tnode {     int data;      struct tnode * left;     struct tnode * right; };  struct tnode * talloc(int data){     struct tnode * newTnode;     newTnode = (struct tnode *) malloc (sizeof(struct tnode));     newTnode -> data = data;     newTnode -> left = NULL;     newTnode -> right = NULL;     return newTnode; } 
like image 23
paxdiablo Avatar answered Oct 05 '22 00:10

paxdiablo