Welcome Guest ( Log In | Register)



 
Closed TopicStart new topic
> C Problem: "two Or More Data Types In Declaration", also need help with malloc
beeseven
post Feb 11 2006, 10:54 PM
Post #1


Privileged Member
*********

Group: Members
Posts: 629
Joined: 26-February 05
Member No.: 3,995



What does it mean when I get the error
QUOTE
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 >_>

This post has been edited by beeseven: Feb 13 2006, 05:32 PM
Go to the top of the page
 
+Quote Post
switch
post Feb 17 2006, 02:48 AM
Post #2


Premium Member
********

Group: Members
Posts: 178
Joined: 13-October 04
From: NSW, Australia
Member No.: 1,713



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:
CODE
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:
CODE
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:
CODE
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 wink.gif

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

ok! good luck mate!

This post has been edited by switch: Feb 17 2006, 03:04 AM
Go to the top of the page
 
+Quote Post
beeseven
post Feb 18 2006, 05:27 AM
Post #3


Privileged Member
*********

Group: Members
Posts: 629
Joined: 26-February 05
Member No.: 3,995



Makes sense, but unless I'm missing something here it can't be the answer. Here's the function:
CODE
void setval(treenode a, int b)
{
    a->val = b;
}
Not too complicated. treenode is defined earlier:
CODE
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.
Go to the top of the page
 
+Quote Post
bidarshi
post Mar 23 2006, 02:32 PM
Post #4


Newbie
*

Group: Members
Posts: 8
Joined: 23-March 06
Member No.: 20,552



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.
Go to the top of the page
 
+Quote Post
iGuest
post Oct 17 2007, 03:44 PM
Post #5


Trap Double Mocha Member
***************

Group: Members
Posts: 2,360
Joined: 21-September 07
Member No.: 50,369



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.
Go to the top of the page
 
+Quote Post
iGuest
post Apr 16 2008, 08:24 PM
Post #6


Trap Double Mocha Member
***************

Group: Members
Posts: 2,360
Joined: 21-September 07
Member No.: 50,369



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
Go to the top of the page
 
+Quote Post
iGuest
post Jun 5 2008, 02:36 PM
Post #7


Trap Double Mocha Member
***************

Group: Members
Posts: 2,360
Joined: 21-September 07
Member No.: 50,369



You're missing a semi-colon after the structure.


Struct atreenode {
int val;
treenode left, right;
} ; // here

I had the same error, only with : enum { something, ..} ; // I was missing the semi-colon

-reply by mikea
Go to the top of the page
 
+Quote Post

Closed TopicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Basic Data Types And Operators(0)


 



- Lo-Fi Version Time is now: 26th July 2008 - 03:22 PM