![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
I think I figured out my pointer problem.
I was doing something like (pseudocode here):
int* bullet.x;
int* ship.x;
bullet.x = ship.x;
What this is doing is assigning the memory address of ship.x to bullet.x...
So when I change ship.x so does bullet.x -- this is not what we want.
We want to initialize the value of bullet.x to start at the same spot the ship is, but then when the ship moves, bullet.x should not change with the ship. Because it is now on its own trajectory.
So to copy the value, and not point to the memory occupied by the ship.x, we need to dereference (god, this is why it sucks to go back to programming after a hiatus, all things you should know easily forgotten, especially with pointers):
bullet.x = *(ship.x)
This dereferences the memory address, and tells it actually assign the VALUE not the ADDRESS of ship.x. Now, if ship.x changes, the value itself stays, since bullet.x now has it's own copy of the data, and is not just pointing to ship.x
At least, a cursory glance at C shit seems to indicate this is how it works. I forgot it all since it's been like half a year since I last touched code like this in any detail.
Anybody smarter at programming that can confirm if this is right???
I was doing something like (pseudocode here):
int* bullet.x;
int* ship.x;
bullet.x = ship.x;
What this is doing is assigning the memory address of ship.x to bullet.x...
So when I change ship.x so does bullet.x -- this is not what we want.
We want to initialize the value of bullet.x to start at the same spot the ship is, but then when the ship moves, bullet.x should not change with the ship. Because it is now on its own trajectory.
So to copy the value, and not point to the memory occupied by the ship.x, we need to dereference (god, this is why it sucks to go back to programming after a hiatus, all things you should know easily forgotten, especially with pointers):
bullet.x = *(ship.x)
This dereferences the memory address, and tells it actually assign the VALUE not the ADDRESS of ship.x. Now, if ship.x changes, the value itself stays, since bullet.x now has it's own copy of the data, and is not just pointing to ship.x
At least, a cursory glance at C shit seems to indicate this is how it works. I forgot it all since it's been like half a year since I last touched code like this in any detail.
Anybody smarter at programming that can confirm if this is right???
no subject
Date: 2009-03-24 08:30 pm (UTC)One big problem is that if those aren't actually pointers to ints but pointers to a data structure, and that data structure contains more pointers, dereferencing the top-level pointer won't dereference the lower-level items. To do that, you need to implement a deep copy function yourself.
no subject
Date: 2009-03-25 12:10 am (UTC)bullet->x = *(ship->x)
Yet, I'm getting an error :(
(invalid type argument of `unary *') Hmm...
I'll keep plugging away. Fuckin' pointers. Thanks for the reply, though.
no subject
Date: 2009-03-25 12:16 am (UTC)foo->bar
Is actually shorthand for:
*(foo.bar)
no subject
Date: 2009-03-25 04:48 am (UTC)http://www.storytron.com/index.php
Apparently it's a project from Chris Crawford.
It's some sort of programming language/toolkit designed for creating stories/games.
I figured you've been on an IF kick recently, and with your love of programming, maybe this might interest you.
Saw it on boingboing.