用memcpy函数赋值数组中间某段数据,写个例程
时间: 2023-06-30 11:06:08 浏览: 47
以下是一个使用memcpy函数赋值数组中间某段数据的例程:
```c
#include <stdio.h>
#include <string.h>
int main() {
int arr1[] = {1, 2, 3, 4, 5};
int arr2[5];
int i;
memset(arr2, 0, sizeof(arr2)); // 将arr2数组清零
memcpy(arr2 + 1, arr1, 3 * sizeof(int)); // 将arr1数组中索引为0~2的元素复制到arr2数组中索引为1~3的位置
for (i = 0; i < 5; i++) {
printf("%d ", arr2[i]);
}
printf("\n");
return 0;
}
```
该程序的输出结果为:
```
0 1 2 3 0
```
可以看到,使用memcpy函数将arr1数组中索引为0~2的元素复制到arr2数组中索引为1~3的位置,而中间的元素被清零了。
相关问题
用memcpy函数赋值数组中间某段数据,在将该段数据完整的显示出来,写个例程
以下是一个使用memcpy函数赋值数组中间某段数据,并将该段数据完整显示出来的例程:
```
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello, world!";
char str2[] = "CSDN AI";
int start = 7;
int len = strlen(str2);
memcpy(str1 + start, str2, len);
printf("%s\n", str1);
return 0;
}
```
该程序将字符串"Hello, world!"中的第7个字符开始的一段数据替换为"CSDN AI",并将替换后的字符串完整地显示出来。输出结果为:"Hello, CSDN AI!"。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![dll](https://img-home.csdnimg.cn/images/20210720083646.png)
![md](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)