您将如何显示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 红包祝福语简短10字
18 周六早晨祝福语简短