在项目中看到下面一个程序,出于好奇记录一下。
对数组进行强制类型转换,并通过指针修改数组参数
#include <stdio.h>
#include <stdint.h>
int main() {
// 定义U8类型的数组
uint8_t u8Array[7] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36 };
// 将U8数组强制转换为U32类型的指针
uint32_t* u32Array = (uint32_t*)u8Array;
printf("Printing u8Array:\n");
// 打印U8数组中的每个元素和地址
for (int i = 0; i <= sizeof(u8Array); i++) {
printf("u8Array[%d] = 0x%X, Address: %p\n", i, u8Array[i], (void*)&u8Array[i]);
}
printf("\nPrinting u32Array:\n");
// 打印U32数组中的每个元素和地址
for (int i = 0; i <= sizeof(u8Array) / sizeof(uint32_t); i++) {
printf("u32Array[%d] = 0x%X, Address: %p\n", i, u32Array[i], (void*)&u32Array[i]);
}
return 0;
}
程序中定义了一个数组,存放了7个值,每个值占用一个字节的空间。
随后重新声明了一个32位的指针变量,值为数组的首地址。
u32类型在输出的时候会输出4个字节。
低字节存放在低地址-小端模式
输出结果
Printing u8Array:
u8Array[0] = 0x30, Address: 0000000A0299F714
u8Array[1] = 0x31, Address: 0000000A0299F715
u8Array[2] = 0x32, Address: 0000000A0299F716
u8Array[3] = 0x33, Address: 0000000A0299F717
u8Array[4] = 0x34, Address: 0000000A0299F718
u8Array[5] = 0x35, Address: 0000000A0299F719
u8Array[6] = 0x36, Address: 0000000A0299F71A
u8Array[7] = 0xCC, Address: 0000000A0299F71B
Printing u32Array:
u32Array[0] = 0x33323130, Address: 0000000A0299F714
u32Array[1] = 0xCC363534, Address: 0000000A0299F718
在没有创建新数组的情况下,与原数组公用一个地址存放数据
修改数组值
#include <stdio.h>
#include <stdint.h>
int main() {
// 定义U8类型的数组
uint8_t u8Array[7] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36 };
// 将U8数组强制转换为U32类型的指针
uint32_t* u32Array = (uint32_t*)u8Array;
printf("Printing u8Array:\n");
// 打印U8数组中的每个元素和地址
for (int i = 0; i <= sizeof(u8Array); i++) {
printf("u8Array[%d] = 0x%X, Address: %p\n", i, u8Array[i], (void*)&u8Array[i]);
}
printf("\nPrinting u32Array:\n");
// 打印U32数组中的每个元素和地址
for (int i = 0; i <= sizeof(u8Array) / sizeof(uint32_t); i++) {
printf("u32Array[%d] = 0x%X, Address: %p\n", i, u32Array[i], (void*)&u32Array[i]);
}
u32Array[0] = 0x31323334;
printf("\nNew Printing u8Array:\n");
// 打印U8数组中的每个元素和地址
for (int i = 0; i <= sizeof(u8Array); i++) {
printf("u8Array[%d] = 0x%X, Address: %p\n", i, u8Array[i], (void*)&u8Array[i]);
}
printf("\nNew Printing u32Array:\n");
// 打印U32数组中的每个元素和地址
for (int i = 0; i <= sizeof(u8Array) / sizeof(uint32_t); i++) {
printf("u32Array[%d] = 0x%X, Address: %p\n", i, u32Array[i], (void*)&u32Array[i]);
}
return 0;
}
输出结果
Printing u8Array:
u8Array[0] = 0x30, Address: 000000457CB8F974
u8Array[1] = 0x31, Address: 000000457CB8F975
u8Array[2] = 0x32, Address: 000000457CB8F976
u8Array[3] = 0x33, Address: 000000457CB8F977
u8Array[4] = 0x34, Address: 000000457CB8F978
u8Array[5] = 0x35, Address: 000000457CB8F979
u8Array[6] = 0x36, Address: 000000457CB8F97A
u8Array[7] = 0xCC, Address: 000000457CB8F97B
Printing u32Array:
u32Array[0] = 0x33323130, Address: 000000457CB8F974
u32Array[1] = 0xCC363534, Address: 000000457CB8F978
New Printing u8Array:
u8Array[0] = 0x34, Address: 000000457CB8F974
u8Array[1] = 0x33, Address: 000000457CB8F975
u8Array[2] = 0x32, Address: 000000457CB8F976
u8Array[3] = 0x31, Address: 000000457CB8F977
u8Array[4] = 0x34, Address: 000000457CB8F978
u8Array[5] = 0x35, Address: 000000457CB8F979
u8Array[6] = 0x36, Address: 000000457CB8F97A
u8Array[7] = 0xCC, Address: 000000457CB8F97B
New Printing u32Array:
u32Array[0] = 0x31323334, Address: 000000457CB8F974
u32Array[1] = 0xCC363534, Address: 000000457CB8F978