输出字节序列的地址

#include <stdio.h>

typedef unsigned char *byte_pointer; //将数据类型byte_pointer定义为一个指向类型为unsigned char的对象的指针

void show_bytes(byte_pointer start, size_t len) { //输入为一个字节序列的地址,和一个字节数
    size_t i;
    for (i = 0; i < len; i++)
        printf("%.2x", start[i]);
    printf("\n");
}

void show_int(int x) {
    show_bytes((byte_pointer) &x, sizeof(int)); //读入int x的地址和字节数,然后通过调用函数show_bytes来输出字节序列的地址
}

void show_float(float x) {
    show_bytes((byte_pointer) &x, sizeof(float));//读入float x的地址和字节数,然后通过调用函数show_bytes来输出字节序列的地址
}

void show_pointer(void *x) {
    show_bytes((byte_pointer) &x, sizeof(void *));//读入void *x的地址和字节数,然后通过调用函数show_bytes来输出字节序列的地址
}

void test_show_bytes(int val) {
    int ival = val;
    float fval = (float) ival;
    int *pval = &ival;
    show_int(ival);
    show_float(fval);
    show_pointer(pval);
}

int main(void) {
    int val = 12345;
    test_show_bytes(val);//如果函数写在被调用处之前,可以不用声明
}
posted @ 2021-11-07 17:56  里列昂遗失的记事本  阅读(56)  评论(0编辑  收藏  举报