简体   繁体   English

是否可以在屏幕上在C程序中输出某个内存地址的内容?

[英]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? 出于调试原因,C程序中的一个如何打印一些内存地址?

For instance, how should I do to print the content of address 0x611268 in form of a 4 byte float? 例如,我应该如何以4字节浮点数的形式打印地址0x611268的内容?

I know I could use a debugger, but I mean, to print out in screen. 我知道我可以使用调试器,但我的意思是在屏幕上打印出来。

More correctly, printf("Value = %f\\n", *((float*)0x611268)); 更正确地说, 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 . 当然,这假定您提供的地址在运行printf的进程的地址空间中。

打印指针内容的正确格式说明符是“%p”:

fprintf(stderr, "myVar contains %p\n", (void*)myVar);
#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM