A little doubt about structs with pointers

Posts 1–6 of 6 · Page 1 of 1
A little doubt about structs with pointers
Hey,

I have the pointer to the structure of the player, and tested to access the values ​​I wanted, but I got a problem (in fact I'm not sure if it really is one), which consists of the following: I have, for example, the first element in this structure the player's health, the second to the eleventh unknown values​​, and on the twelfth I have the ammo player's primary weapon.

My question is: I really need to add "char unknown" until the twelfth, or have some way to "pass" it? Drawing what I said:

Code:
// Is possible to do this? If yes, how?
typedef struct {
	int HP; // first element
        // second to eleventh are ignored in the struct
	int SecAmmo; // twelfth
} Player_Base;
How I'm having to use (not very good):

Code:
typedef struct {
	int HP;
        char unk2[128]; char unk3[128]; char unk4[128]; char unk5[128]; char unk6[128]; char unk7[128]; char unk8[128]; char unk9[128]; char unk10[128]; char unk11[128];
	int SecAmmo; // 12
} Player_Base;
Thanks in advance.
You need to make sure that your data is properly aligned size wise. Each entry in your structure needs to be properly typed to ensure when you cast from the pointer you are going to get the right alignment back in the structure.

For example, this is how you can pad between the two for 4 bytes:

Code:
typedef struct {
    int HP; // (int = 4 bytes)
    unsigned char[4]; // (unsigned char = 1 byte, so this would total 4 bytes)
    int SecAmmo; // (int = 4 bytes)
} Player_Base;
Play with the size of the buffer between each entry that you are marking as unknown to ensure you have the proper distance.
Hey, thanks for helping. I already do it a few minutes after I posted this topic.

I used this:

typedef struct {
int HP[17]; // 1
int SecAmmo[5]; // 18
int PriAmmo[1]; // 21
} Player_Base;
However, I have another question, can you help me again?

While I alert (show) HP, PriAmmo and SecAmmo it all works (show me the value of the address that I want), but, only in HP address I can change it. e.g:

Player->HP[0] = 100; // new HP = 100, ok
Player->SecAmmo[0] = 50; // not ok, it doesn't change, but MessageBox(... itoa (Player->Sec... )) tell me the right value

Do you know what can be wrong here? Thanks again.
Isn't your pointer pointing to a wrong address ? Or another address like an "copy" of the address that you want? Like another address in game to display text? I get an game with 3 address with the same value, one was the real one, the other two was for text or something like this.
I already solved this, yes, he is appotting to the right address, however, had a protect in this address (i removed and try to changed, it works!).

Thanks for helping.
The way you are doing things is not correct for a structure.. You shouldn't be setting things like:
int HP[17]

This makes 17 ints related to HP so you could have:
HP[0], HP[1] .. HP[16]

From how you are explaining it, there is only the single HP object there so it should just be:
int HP;

And thats even assuming you have the size of the variable correct to begin with. After that you should have padding variable there:
unsigned char Unknown0001[64]; // 4 * 16


So you should have something like:
Code:
typedef struct {
    int HP;
    unsigned char Unknown0001[64];
    int SecAmmo;
    unsigned char Unknown0002[12];
    int PrimeAmmo;
} Player_Base;
Posts 1–6 of 6 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us