size_t 是种无符号整型类型
在32位机中相当于 unsigned int
在64位机中国相当于unsigned long long
可以定义用来使用数组索引和字节大小
大小1个字节
void指针可以任意类型的数据 任意指针都可以想void* 赋值
如果使用void类型指针要赋值给其他类型指针得强制类型转换 规范
void指针不能复引用 也就是不能取内容
这样写是错误的
void指针不能进行算数运算
# include <stdio.h>
typedef unsigned char* byte_pointer;
void show_bytes(byte_pointer start, size_t len)
{
for (size_t i = 0; i < len; i++)
{
printf("%.2x", start[i]);
}
printf("\n");
}
void show_int(int x)
{
show_bytes((byte_pointer)&x, sizeof(int));
}
void show_float(float x)
{
show_bytes((byte_pointer)&x, sizeof(float));
}
void show_pointer(void* x)
{
show_bytes((byte_pointer)&x, sizeof(void*));
}
int main(void)
{
int val = 12345;
show_int(val);
show_float((float)val);
show_pointer((void*)&val);
system("pause");
return 0;
}
typedef unsigned char* byte_pointer;
定义了一个非负类型的字节指针 可以用来输出不同类型的字节数
sizeof()
用于返回一个变量或变量类型占字节数
%.2x
表示使用只是使用两位来表示十六进制数 如果不够两位 直接补0
如 12
直接输出0c
void show_bytes(byte_pointer start, size_t len)
{
for (size_t i = 0; i < len; i++)
{
printf("%.2x", start[i]);
}
printf("\n");
}
这段代码首先传入一个字节类型的变量 根据长度输出循环变量字节
void show_int(int x)
{
show_bytes((byte_pointer)&x, sizeof(int));
}
强制类型转换根据类型大小传入 循环次数
123456的十六进制为0x30390000 输出为0x39300000 由此可知Windows为小端存取
原文:https://www.cnblogs.com/lyraHeartstrings/p/14847657.html