Search code examples
cvariablesmemorymemory-address

How to access , change and print a variable by its memory adress in c?


let's say that i have an integer variable (var) ,and it's memory adress is 1234f , but i dont know the name of this variable and i want to see what value does the memory adress 1234f have , and print it out or change it . is there any possible ways to do this in c ?


Solution

  • How to access , change and print a variable by its memory address in c?

    Assuming the address value is valid:

    First create a int *.

    int *var_pointer = (int *) 0x1234f;
    printf("%d\n", *var_pointer);
    *var_pointer = 42;
    printf("%d\n", *var_pointer);
    

    If address is not valid, the result is undefined behavior (UB).