美文网首页
10.13-2 数组拷贝

10.13-2 数组拷贝

作者: 日常表白结衣 | 来源:发表于2017-07-13 23:36 被阅读0次

三种方式实现数组的拷贝
调用如下函数

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 5
void 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++;
    }
}

相关文章

  • 10.13-2 数组拷贝

    三种方式实现数组的拷贝调用如下函数 程序示例

  • 关于几个拷贝的问题

    数组浅拷贝 数组深拷贝 复合数组深拷贝

  • 浅拷贝与深拷贝

    /*浅拷贝:拷贝地址*/ /*深拷贝:拷贝对象*/ 用Strong修饰不可变数组:浅拷贝 用Copy修饰不可变数组...

  • 11_聊一聊js中实现数组拷贝的常用方法

    一、数组赋值 1、要点 用数组直接赋值的方式实现数组的拷贝,改变拷贝后的数组的元素,被拷贝的数组的元素也会发生改变...

  • lodash中常用的方法

    lodash会拷贝一份新数组,不会对之前的数组进行影响 数据的基础处理 浅拷贝&&深拷贝 数组的分割,将数组(ar...

  • Javascript深拷贝

    什么是深拷贝 创建一个新的对象或数组时,将原对象/数组的“值”拷贝,而不是“引用”。 深拷贝 数组拷贝不存在多层嵌...

  • javascript 杂记

    数组杂记 1)数组的判断,使用Array.isArray() 2)一维数组的深拷贝 3)多维数组的深拷贝 4)数组...

  • 关于OC中数组的深、浅拷贝的小总结

    简而言之:数组的深拷贝,仅仅只是拷贝数组的内容,数组内元素的地址不会变,如果想要数组内的对象元素也深拷贝,则数组内...

  • 关于 CopyOnWriteArrayList 的一个简单优化

    一、优化动机 COW 简介:增删改都会加锁并拷贝工作数组,在拷贝数组上做完增删改操作后,会把拷贝数组切换为工作数组...

  • Android Gradle脚本

    定义变量 定义字典 定义数组 打印 遍历数组拷贝文件+重命名 拷贝文件夹 拷贝+修改文件内容

网友评论

      本文标题:10.13-2 数组拷贝

      本文链接:https://www.haomeiwen.com/subject/pnnghxtx.html