您将如何显示C变量的内存表示形式?
在这里,我们将看到如何打印C变量的内存表示形式。在这里,我们将显示整数,浮点数和指针。
为了解决这个问题,我们必须遵循以下步骤-
获取变量的地址和大小
将地址类型转换为字符指针以获取字节地址
现在循环查找变量的大小,并输出类型转换指针的值。
示例
#include <stdio.h>
typedef unsigned char *byte_pointer; //create byte pointer using char*
void disp_bytes(byte_pointer ptr, int len) {
//这将使用字节指针,并打印内存内容
int i;
for (i = 0; i < len; i++)
printf(" %.2x", ptr[i]);
printf("\n");
}
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 00 00 00 00 40 3c fe 22 00 00 00 00 00 05 00 00 00
热门推荐
10 分别发的祝福语简短
11 大外甥高考祝福语简短
12 小学毕业校长简短祝福语
13 搞工程发财祝福语简短
14 冬季祝福语男生文案简短
15 同事生子问候祝福语简短
16 退休文案寄语简短祝福语
17 送给自己的祝福语简短
18 简短祝福语毛笔字