Skip to main content
GameDev.net gamedev.net
🔒 Locked

MALLOC

Started by TheShadow Mar 23, 2003 at 6:28 PM 3 replies 200+ views
Original Post
TheShadow
TheShadow
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?
Mecha Engineer (Making Real Humanoid Suits)
falkone
falkone
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]
Disclaimer: "I am in no way qualified to present advice n any topic concerning anything and can not be held responsible for any damages that my advice may incurr (due to neither my negligence nor yours)" o
TheShadow
TheShadow
im using

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


apuntador_nuevo = malloc(sizeof(NODO));
Mecha Engineer (Making Real Humanoid Suits)
TheShadow
TheShadow
Thanks i was just reading about casts when you gave me the answer :D saved me a bit of thinking
Mecha Engineer (Making Real Humanoid Suits)
smart_idiot
smart_idiot
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.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.