10.13-2 数组拷贝
来源:互联网 发布:高要网络问政平台 编辑:程序博客网 时间:2024/05/27 14:14
三种方式实现数组的拷贝
调用如下函数
const double source[SIZE] = { 1.1,2.2,3.3,4.4,5.5 };double target1[SIZE] = { 0 };double target2[SIZE] = { 0 };double target3[SIZE] = { 0 };copy_arr(target1, source, 5);copy_ptr(target2, source, 5);copy_ptrs(target3, source,source+SIZE);
程序示例
/* 10.13.2 */#include<stdio.h>#define SIZE 5void copy_arr(double target1[], const double source[], int m);void copy_ptr(double *target2, const double *source, int m);void copy_ptrs(double *target3, const double *source, const double *end);int main() { const double source[SIZE] = { 1.1,2.2,3.3,4.4,5.5 }; double target1[SIZE] = { 0 }; double target2[SIZE] = { 0 }; double target3[SIZE] = { 0 }; printf(" the orgin arry:\n "); for (int i = 0; i < SIZE; i++) printf("%8.3lf", source[i]); putchar('\n'); copy_arr(target1, source, 5); copy_ptr(target2, source, 5); copy_ptrs(target3, source,source+SIZE); for (int i = 0; i < SIZE; i++) printf("%8.3lf ", target3[i]); putchar('\n'); getchar(); return 0;}void copy_arr(double target1[],const double source[], int m){ printf(" the arry target1:\n"); for (int i = 0; i < m; i++) { printf("%8.3lf", target1[i] = source[i]); } putchar('\n');}void copy_ptr(double *target2, const double *source, int m){ printf(" the arry target2:\n"); for (int i = 0; i < m; i++) { //此处若为 target2+i = source+i 则属于const指针赋予非const指针,是不安全的 //因此,采用对存储在地址的数据值传递的方式进行操作 printf("%8.3lf",*(target2 + i) = *(source + i)); } putchar('\n');}void copy_ptrs(double *target3, const double *source, const double *end){ printf(" the arry target3:\n"); while (source < end) { *target3=*source; source++; target3++; }}
阅读全文
0 0
- 10.13-2 数组拷贝
- 数组拷贝
- 数组拷贝
- 数组拷贝
- 拷贝数组
- 数组拷贝
- 拷贝数组
- 数组拷贝
- IO流(2)--定义小数组拷贝
- java拷贝数组(深拷贝)
- 另类的数组拷贝
- 数组深拷贝
- 字符数组拷贝
- Java 数组 的 拷贝
- java中数组拷贝
- java中拷贝数组
- Java 数组的拷贝
- 事件,拷贝数组
- 欢迎使用CSDN-markdown编辑器
- 9.3 函数 :任意进制输出[递归]
- 10.3 指针和数组
- 10.3 指针
- 10.7 指针 :指针和多维数组
- 10.13-2 数组拷贝
- $ref':'$[0].coupon.merchants[0]' ,数据引用问题
- 调用pcl计算法向量,并将法向量可视化
- 10.13-3 指针与一维数组相关运算
- (1)JVM运行时数据分区
- 10.13-7 指针与二维数组copy
- 11.1 字符串和指针
- jquery 点击元素实现背景色改变,点击元素以外任意地方还原该元素背景色
- 11.2 字符串和字符串函数:fgets()函数