用memcpy函数赋值数组中间某段数据,写个例程
时间: 2023-07-04 19:31:07 浏览: 41
下面是一个使用memcpy函数赋值数组中间某段数据的例程:
```c
#include <stdio.h>
#include <string.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int len = sizeof(arr) / sizeof(int);
int start = 1; // 要复制的起始位置
int end = 3; // 要复制的结束位置
int count = end - start + 1; // 要复制的元素个数
int new_arr[count]; // 新数组,用来存放复制的数据
memcpy(new_arr, arr + start, count * sizeof(int)); // 复制数据
// 打印原数组
printf("原数组:");
for (int i = 0; i < len; i++) {
printf("%d ", arr[i]);
}
printf("\n");
// 打印新数组
printf("新数组:");
for (int i = 0; i < count; i++) {
printf("%d ", new_arr[i]);
}
printf("\n");
return 0;
}
```
输出结果为:
```
原数组:1 2 3 4 5
新数组:2 3 4
```
相关问题
用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)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)