I presently trying to learn how to write in memory in various ways, i.e. I'm playing.
I've created a struct as follows:
struct strProjectile
{
int Damage;
int ParticlesPerSecond;
short Fire;
short Plasma;
};
I've then created an array as follows:
strProjectile Projectiles[40];
From that array, I've then created a pointer to point at the beginning of the array:
strProjectile *ptrProjectiles;
ptrProjectiles = Projectiles;
Using that pointer, you can set the values in the variables like so (I know this can be done directly not using the pointer too using the . notation):
ptrProjectiles->Damage = 0;
ptrProjectiles->ParticlesPerSecond = 0;
ptrProjectiles->Fire = 0;
ptrProjectiles->Plasma = 0;
I have also found I can set the values in the variables by creating a pointer to each of the variables in each struct of the array. For example, the following would set Damage to 0:
int *Damage = (int *)ptrProjectiles;
*Damage = 0;
My question is can I use the pointer to the array directly to set each variable within the struct directly in memory as I did using the separate pointer for damage above? Using something like:
*ptryProjectiles = 0;
then skipping 4 bytes of memory to get to the next variable in the struct (in this case, ParticlesPerSecond), set that, then skip 4 bytes more and set the next variable (in this case, Fire), then skip the next 2 bytes and set the next variable (in this case, Plasma), then skip 2 more bytes to get the next object in the array and then repeat....
I cant see a reason I need to do this, but just want to know if it can be done for my full understanding....
Thanks in advance!