Advertisement

Do dynamic pointers exist?

Started by May 24, 2001 07:11 PM
4 comments, last by GameCreator 23 years, 8 months ago
Say I have two structures. struct object { int active; }; struct creature { int active; }; Is there a way to make a pointer that can point to either one on demand? I''m new to pointers so I''m not sure how this would work or even if it is possible. In main() you would declare everything. struct object object1; struct creature creature1; struct pointer *ptr; // ??? Then you would somehow make the *ptr point to one or the other at your will. And if the object or creature is to not be accessible anymore (it dies in the game, for example) then you could just do something like ptr->active=0; and it wouldn''t matter if it was an object or creature. It would do it. Can this be done. If so, how? If anyone needs an explanation as to why I need to do this I will let you know. It involves those two structures and linked lists. Thanks for any help.
Just use a void pointer, and cast it to whatever struct it actually points to.

~~~~~~~~~~
Martee
http://www.csc.uvic.ca/~mdill
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
Advertisement
you could use inheritance

struct Object
{
int active;
};

struct Creature : public Object
{
};


//main

Creature MyCreature;
Object* pObject;

pObject = &MyCreature
pObject->active = false;

Magmai Kai Holmlor
- The disgruntled & disillusioned


Edited by - Magmai Kai Holmlor on May 24, 2001 8:30:34 PM
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Ok. Here's the program I was working with thanks to the suggestion of Martee. It would compile except for the error at the line

printf("ptr.health: %d\n",ptr.health);

Apperantly it has no idea what ptr is.

  #include <conio.h>struct player{   int health;};struct enemy{   int health;};main(){   struct player player1;   struct enemy enemy1;   void           *ptr;   player1.health = 100;   enemy1.health  = 50;   clrscr();   printf("Player1.health: %d\n",player1.health);   printf("Enemy1.health:  %d\n",enemy1.health);   // Is this right?   ptr = &(struct player)player1;   // Now I want to access health in ptr   // But instead I get   // Error: request for member `health'   // in something not a structure or union   printf("ptr.health:  %d\n",ptr.health);   getch();}   


I know this is kind of a beginner thing. I'm not too familiar with pointers though I think the casting is ok. What is that last printf line supposed to look like? Thanks for any help.

By the way, I work with DJGPP and C only, not C++. Also I wanted an extra, individual pointer to point to one or the other structure. Thank you for your response though, Magmai Kai Holmlor.


Edited by - GameCreator on May 24, 2001 11:56:28 PM
You''ll want to use ptr->health instead of ptr.health

~~~~~~~~~~
Martee
http://www.csc.uvic.ca/~mdill
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
struct object object1;
struct creature creature1;
void *ptr;

ptr = &object1

ptr points to object1, when you want to use object1 through ptr do

(object *)ptr->active

which is the equivalent of object1.active

ptr = &creature1

ptr points to creature1, use it like this

(creature *)ptr->active

i think thats right, maybe the syntax is a bit off, i havent done any programming for a while

Edited by - outRider on May 24, 2001 12:03:17 AM

This topic is closed to new replies.

Advertisement