Dynamically allocating array of SKeyMap

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Endar
Posts: 145
Joined: Mon Jun 14, 2004 7:59 am

Dynamically allocating array of SKeyMap

Post by Endar »

Okay, if you look at my code and wonder why I'm using some standard C functions instead of any more advanced C++ ones, its cause I'm not fully up to speed on C++, so please take pity. :D

Right, I'm just creating a few functions that will read info from a text file and define the keyboard keys for controling a FPS camera based on the info.

So, I'm using 'malloc' to dynamically allocate space for an array of 'SKeyMap', but when I try to assign it to a 'SKeyMap*' pointer. This is my code:

Code: Select all

 
SKeyMap* keyMap = NULL;
keyMap = (SKeyMap) malloc( sizeof(SKeyMap) * key_num ); 
'keyMap' is the 'SKeyMap*'
'key_num' is an int that holds the number of keys to be assigned from the file being read.

And the error given is: " error C2440: 'type cast' : cannot convert from 'void *' to 'struct irr::SKeyMap' "

I understand the error, but am not sure how to fix it. Ideas??
Asterisk Man
Posts: 62
Joined: Wed Jun 09, 2004 5:51 am

Post by Asterisk Man »

you're typecasting malloc to the wrong data type, it should be (SKeyMap *)
Endar
Posts: 145
Joined: Mon Jun 14, 2004 7:59 am

Post by Endar »

Of course!!!!

And I think I can program!!! :D

Thanks.
"The reputation of a thousand years may be determined by the conduct of one hour."
-Japanese Proverb
Asterisk Man
Posts: 62
Joined: Wed Jun 09, 2004 5:51 am

Post by Asterisk Man »

dont sweat it, I was also fooled by that stupid function lol

PS: try using calloc, it's the same as malloc, but also initializes all the elements in the array to 0!
Post Reply