Jump to content
  • Advertisement

Archived

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

bartkusa

Help me before I poke my eye out with a pointer...

This topic is 5924 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''ve never used one of these things before, but I vaguely understand the principle. Here''s some vaguely-C++-ish looking code that illustrates what I''m doing:
  
struct ship
{
   float position, velocity, otherstuff....;
}

ship Ship1, Ship2;
ship *me, *enemy;

main()
{
   Ship1.position = 2382.32.....;
   // Continue initializing each ship''s parameters...

   Ship2.otherstuff = 1000000;

   me = &Ship1;
   enemy = &Ship2;

   cout << "Current player''s position: " << *me.position;
   // And so on and so forth

}
  
I understand pointers much in the same way a math or physics student learns new formulae or methods without really understanding what they mean. I''m guessing the nature of structures and the nature of pointers fundamentally conflict, but I''m hoping I''m wrong so I can save myself a lot of work Bartikus

Share this post


Link to post
Share on other sites
Advertisement
I'm not sure what exactly you're asking here, but here goes:

First off, structs and pointers are 2 totally different concepts. Structs allow you group related variables under one name. Pointers are variables that contain a memory address and are used for dynamic memory allocation, pass by reference (only way to in C), and other wonderful things.

quote:

I'm guessing the nature of structures and the nature of pointers fundamentally conflict



I have no idea where you came up with that assumption. Pointers and structs have nothing (inherently) to do with each other. How could their nature conflict?

In addition, *me.position will not work correctly because of the operator precedence. (*me).position will, or even better, use me->position (does the same thing, just a simpler syntax).

/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/

[edited by - Chem0sh on May 30, 2002 1:47:26 AM]

Share this post


Link to post
Share on other sites
im assuming youre new to C++, and pointers can be a bit tricky at first. so here we go.
no, the nature of pointers and structs do not fundamentally conflict. if they did, we''d all be screwed . you just need to understand them better. once you do, youll be glad that you took the time. they become incredibly useful for dynamic allocation, and for most data structures.
a pointer is nothing more than a variable(generally 4 bytes) that holds the address of another variable. to access what it points to, use the * operator. UNLESS it points to some object, and you want to access one of its data members.
whenever you access a data member of a struct or class through a pointer to it, do it like this: name of pointer->name of member. so, instead of *me.position, do this: me->position.
hope i could help

Share this post


Link to post
Share on other sites
All pointers are chunks of memory themselves. Each pointer has a number of bytes that store the address of the data they point to. For instance, you have a structure that contains 16 bytes (your ship structure for example), which may be contained at the memory address 0x1000, such as this:

1000: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f

The pointer to this memory resides in another memory address (0x2000 in this example):

2000: 00 00 10 00

The four bytes stored in the pointer''s memory area is 0x00001000, which is the memory address of the structure data. Now, there are a few ways to use the pointer to access it''s own memory and the memory where the pointer is pointing.

ship ShipData;
char *DataPtr = &ShipData;

The above sets up what I''m demonstrating. ShipData is the 16-byte data located at 0x1000. DataPtr is placed at 0x2000 and contains 00 00 10 00. Check out what happens when you play with DataPtr:

*DataPtr = 0x10;

This will change the memory that the pointer is pointing at to:

1000: 10 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f

DataPtr = 0x10203040;

This will change DataPtr''s memory (located at 0x2000) to:

2000: 10 20 30 40

This means that DataPtr no longer points to the structure''s data (ShipData), as it points to some other chunk of memory.

long DataAddress = &DataPtr;

The DataAddress will now contain the pointer''s memory address (0x2000 in this case).

char Byte = *DataPtr;

Assuming DataPtr still points to the structure data, Byte will contain the byte from memory location 0x2000.

DataPtr = &ShipData;

This will change DataPtr''s memory to point to ShipData''s memory space (remember that ShipData is located at 0x1000):

2000: 00 00 10 00



It''s the middle of the night, so I''m typing on empty, but I hope this helps





Jim Adams
home.att.net/~rpgbook
Author, Programming Role-Playing Games with DirectX

Share this post


Link to post
Share on other sites
OK, my question has been answered: don''t use *me.position, but me->position. I was getting error messages.

And the reason I thought there was some sort of conflict is because I didn''t know if the pointers were "smart" enough, as it were, to look up one of the variables in a structure, but would rather treat it as one huge value. But now I know better.

Thanks a lot!

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!