Pelles C forum

C language => Beginner questions => Topic started by: bitcoin on March 01, 2021, 01:55:00 PM

Title: Stupid question about pointers
Post by: bitcoin on March 01, 2021, 01:55:00 PM
Hello, I can't understand

Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

int e_p()
{
LPBYTE PE = HeapAlloc(GetProcessHeap(),0,1024);
DWORD *pointer,*pointer2 = 0;


if(PE != NULL)
{
pointer = (DWORD*)(PE + 0xC);
pointer2 = (DWORD*)(PE + 0xC); //pointer 2 equal pointer
pointer2 = (DWORD*)PE + 0xC; //no equal!
}

ExitProcess(0);
}

Why pointer != pointer2 in last line?  ??? I thought it would be equal, but no.
Title: Re: Stupid question about pointers
Post by: frankie on March 01, 2021, 04:27:16 PM
Because in
Code: [Select]
pointer2 = (DWORD*)(PE + 0xC);'PE' is a byte pointer, to which you add 0xC and by the pointers arithmetic you get 'PE = PE + (sizeof(BYTE) * 0xC)'.
Then you cast 'PE' to a pointer to 'DWORD'.
In the second case
Code: [Select]
pointer2 = (DWORD*)PE + 0xC;First you cast 'PE' to a 'DWORD' pointer then add the offset. In this case the pointers arithmetic works differently: 'PE = PE + (sizeof(DWORD) * 0xC)'. And because the size of a DWORD=4, you're adding 0xC*4.
Title: Re: Stupid question about pointers
Post by: bitcoin on March 01, 2021, 05:20:57 PM
Thank you frankie !