Jump to content
  • Advertisement
Sign in to follow this  
cornflake

Question about * usage (pointers?)

This topic is 4255 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

I'm looking through some code examples, and although I've been working through a C++ tutorial and think I've got the hang of pointers I've come across something I don't understand. The code I'm looking at is a player class from the Wrathlands source -
class PLAYER: public ENTITY
{
private:
	int strength;
	int intelligence;
	int level;
	WEAPON weapon;
	ARMOR armor;
	POTION* potionlist[15];
	SPELL* spelllist[10];
public:
	PLAYER();
	void SetStrength(int newstr);
	int GetStrength();
	void SetIntelligence(int newint);
	int GetIntelligence();
	void SetLevel(int newlevel);
	int GetLevel();

	WEAPON* GetWeapon();
	void SetWeapon(WEAPON weapon);
	ARMOR* GetArmor();
	void SetArmor(ARMOR armor);

	void AddPotion(POTION* potion);
	POTION** GetPotionList();

	void AddSpell(SPELL* newspell);
	SPELL** GetSpellList();

	void CheckForLevelUp();
};
What I don't get is the lines like
POTION* potionlist[15];
as in the pointer tutorial it didn't have things with the asterix on the end of a word like this. Can someone explain what this does? It may weel be something I haven't covered yet but I'm trying to follow the things I learn in examples like this so I can relate what I learn to real world uses, as this helps me learn things a lot better.

Share this post


Link to post
Share on other sites
Advertisement
That means that the type of object being stored in an array is a pointer to a POTION. That differs from storing actual potions, for example.

Share this post


Link to post
Share on other sites
POTION* potionlist[15];

This is an array of 15 pointers to POTION structs.

Quote:

as in the pointer tutorial it didn't have things with the asterix on the end of a word like this.


Are you sure? That's how pointers are declared, it would be hard to do a pointer tutorial without the * operator.

Share this post


Link to post
Share on other sites
I can only guess the tutorial only presented the asterisk in one manner and never explained the location didn't matter.

These are all equivalent:

int * potion1;
int*potion2;
int
*
poition3;
int *potion4;


That is... the location of the '*' doesn't matter, it merely has to be between the type and the identifier.

Share this post


Link to post
Share on other sites
EDIT: Nobodynews, just saw your post, I think that's probably the issue.

Quote:
Original post by Scet
POTION* potionlist[15];

This is an array of 15 pointers to POTION structs.

Quote:

as in the pointer tutorial it didn't have things with the asterix on the end of a word like this.


Are you sure? That's how pointers are declared, it would be hard to do a pointer tutorial without the * operator.


It did have the * operator, but not used in the way above. I'll re-read through the bit on pointers to arrays, and see if I can find another tutorial as well, as lookig at the source I think I need to make sure I understand pointers fully.

The examples for declaring pointers were -

int * number;
char * character;
float * greatnumber;


Share this post


Link to post
Share on other sites
Keep in mind pointer is usually used to save memory transfer. Let's assume the POTION struct take a massive amount of space (which is probably not really the case for a potion). The potions available in the game get allocated dynamically when the game start. Some of them are given to the player inventory, but not the whole potion get deleted from the map/re-allocated in the player inventory, only the potion pointer get transfered. Same thing if a player give a potion to another player, only the address (which you got when allocating) get transfered and not the whole potions struct and all it's variable. So the memory used by the potion isn't stored IN the player inventory class, but beside it.

Also keep in mind if a potion get used/player die/game finish you need to delete the potion stored in memory before setting the pointer to 0.

Share this post


Link to post
Share on other sites
Quote:
Original post by cornflake
It did have the * operator, but not used in the way above. I'll re-read through the bit on pointers to arrays, and see if I can find another tutorial as well, as lookig at the source I think I need to make sure I understand pointers fully.


POTION* potionlist[15] isn't a pointer to an array, it's an array of pointers. They're quite different! :)



Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!