Jump to content
xisto Community
Sign in to follow this  
beeseven

C Problem: "two Or More Data Types In Declaration" also need help with malloc

Recommended Posts

What does it mean when I get the error

two or more data types in declaration of (function name)

It's almost the exact same as a function I have in another program that compiles and works fine.

 

Also, can someone explain malloc or give me a tutorial? Like where and how you use it, and if there are any restrictions to where you can use it.

 

Notice from KuBi:
Shortened topic title to fix page distortion on the home page.

Edit: Made new title make sense >_>

Edited by beeseven (see edit history)

Share this post


Link to post
Share on other sites

after my first useless post, i just read your post properly and edited accordingly. what is happening is you have a function that has two data types declared, for example:

int char myFunction();
that's my understanding of the compiler error (as int and char are two different kinds of data type).

if you only want to return one kind of data type, then you would just take out the data type you don't want.
if you want to use multiple data types, use this method:
make the function return a pointer to void, i.e. do this:
void *myFunction();
that will return a pointer to ANY DATA type you like. you have to be careful with this however, that the calling function knows what data type the pointer holds or at least knows how big the data is. for instance, if myFunction returns an int, and you interpret it as returning a char, you're going to lose 3 bytes of data. be careful when using void pointers.

if you want, after calling the function, you might want to convert the pointer to a typed pointer to avoid type confusion. i.e., if you know that an int is being returned, to avoid any future problems, try something like this:
int* myPointer;myPointer = (*int)myFunction();
(*int) just converts the void pointer returned by myFunction() into an int pointer.
sorry if that confused more than helped :huh:

also, i don't know anything about malloc, as i've never had to use it. sorry ;)

ok! good luck mate!
Edited by switch (see edit history)

Share this post


Link to post
Share on other sites

Makes sense, but unless I'm missing something here it can't be the answer. Here's the function:

void setval(treenode a, int b){	a->val = b;}
Not too complicated. treenode is defined earlier:
typedef struct atreenode *treenode;struct atreenode {	int val;	treenode left, right;}
It's the same as another function I had for listnodes/linkedlists, the only difference is two children as opposed to one.

Share this post


Link to post
Share on other sites

Well let me help out of this problem. Two or more data types in Declaration does not necessarily mean a code like int char function(); It may be the error. But a more serious type of syntax error would be declaring the function to be of one type say int function(); and then during the definition of the function body declaring it to be of say char function(){//body;}. The compiler will report an error in this case and I think this is a more common error among programmers.Check if you have commited the same.Now for malloc function. Malloc is used to dynamically allocate memory for an array say. In normal array declaration say array[10] we are allocating a fixed 10 unit size of the array.In practice it may not be suitable. Sometimes we might need more space and sometime it result in loss of valuable memory space. malloc is used in this case to dynamically allocate the memory. Malloc on success returns the pointer to the created memory location . It must be properly typecast before use as fundamentally malloc returns a void type pointer.

Share this post


Link to post
Share on other sites

The problem here is probably actually that you didn't put a semicolon after the struct atreenode declaration, and it's the last line before your function declaration.

Share this post


Link to post
Share on other sites

two or more data types in declaration of [Definitely a syntax error]

C Problem: "two Or More Data Types In Declaration"

 

Hello,

I had the same problem and I got it resolved ... In my case I had a struct defined in a header file and the compiler pointed me to the struct definition.

I believe the same case with other I.E function or struct...Etc

 

If you have a debugger then you might have resolved by now... But I am sure that there is a syntax error such as a semi-colon in a file which is included before the error point. Please check other headers and source files for syntax error. I had a syntax error in a header file which I've included before the header which contained this struct definition. Good luck

Cheers!!

 

-reply by krish reddy

Share this post


Link to post
Share on other sites

You're missing a semi-colon after the structure.Struct atreenode { int val; treenode left, right;} ; // hereI had the same error, only with : enum { something, ..} ; // I was missing the semi-colon-reply by mikea

Share this post


Link to post
Share on other sites
Thanks a lot!C Problem: "two Or More Data Types In Declaration"

I just spent a while hunting down this problem -- thanks so much for the issue! :-)

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.