Jul 20, 2008

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

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > C/C++ Programming

free web hosting

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

beeseven
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 >_>

Reply

switch
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!

 

 

 


Reply

beeseven
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.

Reply

bidarshi
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.

Reply

iGuest
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.

Reply

iGuest
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

Reply

iGuest
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

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Recent Queries:-
  1. two or more data types - 12.02 hr back. (1)
  2. two or more data types in declaration of «main»| - 17.69 hr back. (2)
  3. two or more data types in declaration - 3.41 hr back. (4)
  4. two or more data types in declaration of ‘main’ - 29.74 hr back. (2)
  5. error: two or more data types in declaration specifiers typedef - 43.42 hr back. (1)
  6. error: two or more data types in declaration specifiers - 12.17 hr back. (4)
  7. two or more data types in declaration of - 42.19 hr back. (5)
  8. two or more data types in declaration specifiers - 47.82 hr back. (1)
Similar Topics

Keywords : c, problem, data, types, declaration, malloc

  1. Basic Data Types And Operators
    very basic : a must for C Coders! (0)
  2. Programming A Malloc
    (1)
    A little background about what I am doing is this: I have an array which holds a linked list
    containing the memory that is currently taken from the system. Each index I have like 16 size
    memory or less can be stored only in index 0, index 1 is for memory inbetween 16-32, index 2 is for
    memory inbetween 32-64, etc all the way up to 2048 which is the max allocatable memory. Now the way
    my malloc is to work is that it first will run through my list to see if any memory is available
    that is greater than or equal to the size I need, if there is any at all it will return it....
  3. How To Retrieve Image Or Data On A Webpage ?
    (2)
    Hi everyone ! I need my little program to access internet web page, then find some data in it.
    I need to manipulate that data and to show the information to the user interface program. I am
    searching for a way to implement this, but I didn't find anything. Is there any tutorial or
    anything about this ? I really need that information. I have to finish this application as soon as
    possible, and it's kinda "urgent" if I can say. If you know anything about this, please help me
    ! Thank You very much for your help ! ....

    1. Looking for c, problem, data, types, declaration, malloc

Searching Video's for c, problem, data, types, declaration, malloc
advertisement



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



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE