Archived

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

s_cloudx

Pointer to a struct

Recommended Posts

s_cloudx    162
I have the following in my header file: #ifndef GLOBAL extern #endif struct { int x,y; int speed; char status; } Player; Now, how do I make a pointer to this struct that is global? so instead of Player.x =0; it will be Player->x =0; and is it true that pointers are faster?

Share this post


Link to post
Share on other sites
Ziphnor    122
You just use:

Player* myPlayer = new Player;
myPlayer->x = 0;

or(*myPlayer).x = 0;

I dont think pointers are faster as such, they are used to avoid passing parameters as values(this is usually bad because then the values have to be copied).

Share this post


Link to post
Share on other sites
Nazrix    307
actually, Player is the name of your struct, so you have to make a variable of type "Player" like this:

    

Player *player1 = new Player; //pointer of type struct Player



//or if you like the old C way you can do this, but new is much cleaner:

Player *player1 = (Player *) malloc(sizeof(Player));


Then you can pass a pointer that is of type Player into functions rather than passing the whole structure Player which is faster.




A CRPG in development...

Need help? Well, go FAQ yourself.




Edited by - Nazrix on February 6, 2002 5:19:09 AM

Share this post


Link to post
Share on other sites
Peter19852001    126
Sorry, do I misunderstand it or something else.

Isn''t Player the name of variable?

struct
{
int x,y;
int speed;

char status;
} Player;

the type is
struct
{
int x,y;
int speed;
char status;
}

and the variable name is Player;

Tell me if I am wrong.

Share this post


Link to post
Share on other sites
s_cloudx    162
Nazrix, your solution works great! But for some odd reason, I get this linker error message:

struct Zero *Player already defined in Myheader.h

I forgot the exact message. I''m not in my pc right now. And what is very odd is that if I set my compiler on Debug, it works fine (it ignores the error). But when I set it to Release, i get the error. How come?

Anyways, here is Myheader.h:

#ifndef GLOBAL
extern
#endif

struct Player
{
int x,y;
int speed;

char status;
};

Hero *Player = new Hero;

All of my files includes Myheader.h. Main.c has #define GLOBAL in it before including Myheader.h.

Share this post


Link to post
Share on other sites
Nazrix    307
you have to use extern in your header file, but still Player is the name of your struct. You can think of it as a data type (like int or char) only you are making the data type yourself

So it should be:
extern Player *Hero = new Player;


Then in your actual source file you use:
Player *Hero = new Player;


extern tells the linker that you're going to use that variable later.




A CRPG in development...

Need help? Well, go FAQ yourself.



Edited by - Nazrix on February 6, 2002 12:38:50 PM

Share this post


Link to post
Share on other sites
s_cloudx    162
Heheh. No wonder I couldn''t get it to work. This is my old code in myheader.h:

----------
#ifndef GLOBALS
extern
#endif

Hero *Player = new Hero;
----------

It should have been like this:

----------
#ifdef GLOBALS
Hero *Player = new Hero;
#else
extern Hero *Player;
#endif
----------

Heheh. Silly me! :-P

Anyways, maraming salamat sa tulong nyo!!! (Thanks for the help!)

Share this post


Link to post
Share on other sites