explicit cast to malloc()

M

mohi

hello everyone ,
as i have noted a lot many times it has been mentioned that we shudn't
cast pointers returned by malloc()
i mean
its been said that use
node * nod=malloc(sizeof(node));

instead of

node * nod=(node *) malloc( sizeof(node));

but when i use the upper way my gcc compiler shows error like

invalid conversion from void* to node*

whats the matter ?

pls help
thank you
mohan
 
K

Keith Thompson

mohi said:
as i have noted a lot many times it has been mentioned that we shudn't
cast pointers returned by malloc()
i mean
its been said that use
node * nod=malloc(sizeof(node));

instead of

node * nod=(node *) malloc( sizeof(node));

but when i use the upper way my gcc compiler shows error like

invalid conversion from void* to node*

whats the matter ?

Does it show an error *like* "invalid conversion from void* to node*",
or does it show that exact error?

I might expect to see an error message about converting from int to
node* if you forgot the mandatory "#include <stdio.h>".

If that doesn't help, read question 7.6 of the comp.lang.c FAQ,
<http://www.c-faq.com/>, and in the future please check the FAQ before
posting. If that doesn't help, read the rest of section 7, and
perhaps the entire FAQ. And if *that* doesn't help, feel free to post
again -- but *please* copy-and-paste your actual complete program and
the exact error message.
 
D

David Resnick

Does it show an error *like* "invalid conversion from void* to node*",
or does it show that exact error?

I might expect to see an error message about converting from int to
node* if you forgot the mandatory "#include <stdio.h>".

You mean <stdlib.h>...

-David
 
C

CBFalconer

mohi said:
as i have noted a lot many times it has been mentioned that we
shudn't cast pointers returned by malloc(). i mean its been
said that use: node * nod=malloc(sizeof(node));
instead of: node * nod=(node *) malloc( sizeof(node));

but when i use the upper way my gcc compiler shows error like
invalid conversion from void* to node*

whats the matter ?

You are using a C++ compiler, not a C compiler. Probably because
you have misnamed the source file, which should be of type .c
(lower case).
 
M

mohi

You mean <stdlib.h>...

-David

sorry guys im really sorry
i did compile it with g++


it becomes so confusing when u have to use both c and c++
is there a better way of dealing with this confusion
:)
mohan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top