开发者

Is it possible to output on screen the content of a certain memory address in a C program?

For debugging reasons, how can one in a C program print some memory address?

For instance, how should I do to print the content of address 0x6112开发者_如何转开发68 in form of a 4 byte float?

I know I could use a debugger, but I mean, to print out in screen.


More correctly, printf("Value = %f\n", *((float*)0x611268));

Of course this assumes the address you've given is in the address space of the process running the printf.


#include <stdio.h>

int main(void)
{
  // pointer to int (4bytes) that points to memory address 0x611268
  int* address = (int *)0x611268; 

  printf("Memory address is: 0x%x\n", address);

  // Note that this address should exist on your process' memory or 
  // the line below will cause a Segmentation Fault
  *address = 0xdead; //assign a value to that address

  printf("Content of that address is: 0x%x\n", *address);

  return 0;
}


The correct format specifier to print out contents of pointers is "%p":

fprintf(stderr, "myVar contains %p\n", (void*)myVar);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜