Archived

This topic is now archived and is closed to further replies.

MALLOC

This topic is 5377 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

My book says you can convert the result of an malloc call (void *) to any type of pointer... i got a (struct lista *) pointer and i can''t seem to do that... it tells me C:\Program Files\Microsoft Visual Studio\MyProjects\addnode\main.cpp(26) : error C2440: ''='' : cannot convert from ''void *'' to ''struct lista *'' Conversion from ''void*'' to pointer to non-''void'' requires an explicit cast Error executing cl.exe. addnode.exe - 1 error(s), 0 warning(s) can anybody help me?

Share this post


Link to post
Share on other sites
try

lista *mystruct;

mystruct = (struct lista *) malloc(stuff....);

EDIT: Then try..

typedef struct lista NODO;
typedef NODO *a_NODO;
a_NODO apuntador_nuevo;


apuntador_nuevo = (struct lista *) malloc(sizeof(NODO));



[edited by - falkone on March 23, 2003 7:37:26 PM]

Share this post


Link to post
Share on other sites
If you were using a C compiler that would have been okay, but your using a C++ compiler.

Your options are:
1) Use a C compiler.
2) Learn C++ instead. For example, using new in place of malloc(), and delete in place of free.
3) Cast everything to the type you intend to use it as. This will probably end up being very annoying both for you and anyone reading your code.

Share this post


Link to post
Share on other sites