I'm new to c programming. There is a use case: I need to change value in a specific memory address.
int main(){
int *p;
p = 0x111;
*p = 100;
return 0;
}
However, I cannot compile the above code. It shows the following problem.
test.c:10:7: warning: incompatible integer to pointer conversion assigning to 'int *' from 'int' [-Wint-conversion]
p = 0x111;
^ ~~~~~
1 warning generated.
I tried casting like following:
int main(){
int *p;
p = (int *) 0x111;
*p = 100;
return 0;
}
It can compile, however, when I run it, it shows segmentation fault
.
My Question:
Is there a way that I can forcedly compile and run this code? I know there is wild pointer problem. However, it's a common use case to manipulate memory. For example, when I play a local game. Somehow, I know the specific memory address of my player's health point when the game is running, e.g. address = 0x123. How to just change the value in 0x123?
If I cannot use above way to manipulate value in any specific memory address, is there other standard way to realize my use case?
PS: By the way, I found the example way to realize my use case.
You can refer to the source code: https://github.com/haseeb-heaven/GTLibc/
How do game trainers change an address in memory that's dynamic?
https://www.youtube.com/watch?v=cRCnN987gd8&ab_channel=HaseebMir