您将如何显示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 生日广州男生祝福语简短