这里我们将看到如何打印 C 变量的内存表示。这里我们将显示整数、浮点数和指针。
要解决这个问题,我们必须遵循以下步骤 –
获取地址和变量的大小将地址类型转换为字符指针以获取字节地址现在循环获取变量的大小并打印类型转换的指针的值。
示例
- #include typedef unsigned char *byte_pointer; //create byte pointer using char*void disp_bytes(byte_pointer ptr, int len) { //this will take byte pointer, and print memory content int i; for (i = 0; i
");}void disp_int(int x) { disp_bytes((byte_pointer) &x, sizeof(int));}void disp_float(float x) { disp_bytes((byte_pointer) &x, sizeof(float));}void disp_pointer(void *x) { disp_bytes((byte_pointer) &x, sizeof(void *));}main() { int i = 5; float f = 2.0; int *p = &i; disp_int(i); disp_float(f); disp_pointer(p); disp_int(i);}
登录后复制
输出
- 05 00 00 0000 00 00 403c fe 22 00 00 00 00 0005 00 00 00
登录后复制
以上就是你将如何展示C变量的内存表示?的详细内容,更多请关注【创想鸟】其它相关文章!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。