unsigned char memory[] = {0x01, 0x00, 0x00, 0x00, 0x3F, 0x80, 0x00, 0x00, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48};取一段赋值给其他变量 C语言代码示例
时间: 2024-03-05 12:46:33 浏览: 24
可以使用指针来取出unsigned char数组中的一段,并赋值给其他变量。以下是C语言代码示例:
```c
#include <stdio.h>
int main() {
unsigned char memory[] = {0x01, 0x00, 0x00, 0x00, 0x3F, 0x80, 0x00, 0x00, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48};
unsigned char subMemory[4]; // 定义一个长度为4的unsigned char数组
// 将memory数组中的一段赋值给subMemory数组
for (int i = 0; i < 4; i++) {
subMemory[i] = memory[i + 4]; // 取出memory数组中索引为4到7的元素,并赋值给subMemory数组
}
// 打印subMemory数组中的值
for (int i = 0; i < 4; i++) {
printf("subMemory[%d] = %02X\n", i, subMemory[i]);
}
return 0;
}
```
运行以上代码,输出结果为:
```
subMemory[0] = 3F
subMemory[1] = 80
subMemory[2] = 00
subMemory[3] = 00
```
这样就将memory数组中索引为4到7的元素赋值给了subMemory数组。