How to get an address in C?

There is probably a very simple answer to this question, but I just do not see it.

#include <stdio.h>

int main(void) {
int x = 0;
printf("\n%d\n%d\n",x,&x);
}

Ok, so printf gives 0 (x) and 2293752 (& x). I'm trying to find a way to look at any address (&) and see what is already stored there. It is just for experimentation. Any help?

+3
source share
6 answers
void* memAddr = (void*)0xAABBCCDD; //put your memory address here
printf("int value at memory address %p is %i", memAddr, *((int*)memAddr));

You will probably find that looking at arbitrary memory addresses will crash.

+4
source

This will dereference the pointer, which is the * operator. However, you cannot tell what type of data is stored for a random address.

#include <stdio.h>

int main(void) {
int x = 0;
printf("\n%d\n%d\n%d\n",x,&x,*(&x));
}

/* resulting output will be 0, some memory address, and 0 again */
+2
source

, , , , , .

, , char int, int 4 , , char.

, 8- ASCII- - , , , - , ​​ base2, base8 base16.

- , - , , ASCII.

, , . , C , . , .

, , , . , , . , , .

, , , . UNIX . , , . - .

!

+2

If you want to learn about memory, I would recommend using a low-level debugger like OllyDbg .

+1
source

Assuming that you want to look at the "raw" data and know the address inside the address space of your process, you should do the following:

long x=<address>
printf("%x", *(int *)x);
0
source

to see the address you can use% p

printf("%p",&x);

I recommend you start reading about pointers, a pointer is a variable that stores a memory address

int x=10;
int *p; //this is a pointer to int
p=&x; //now p has the memory location of x
printf("%d\n",*p); //this will print 10
0
source

Source: https://habr.com/ru/post/1714178/


All Articles