鱼C论坛

 找回密码
 立即注册
查看: 1592|回复: 2

[已解决]拷贝数组遇到的问题

[复制链接]
发表于 2022-10-11 16:10:37 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
拷贝数组:
#include<stdio.h>

void show_arr(const double ar[], int n);

void copy_arr(double target[],const double source[], int n);
void copy_ptr(double * target,const double * source, int n);
void copy_ptrs(double * target, double * source, const double * ar_end);

int main(void){
    double source[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
    double target1[5];
    double target2[5];
    double target3[5];
    copy_arr(target1, source, 5);
    copy_ptr(target2, source, 5);
    copy_ptrs(target3, source, source + 1);
    show_arr(target1, 5);
    show_arr(target2, 5);
    show_arr(target3, 5);
    return 0;
}

void show_arr(const double ar[], int n)
{
    int i;

    for (i = 0; i < n; i++)
    {
        printf("%-5g", ar[i]);
    }
    putchar('\n');
}

void copy_arr(double target[],const double source[], int n){
    int idx;
    for (idx = 0; idx < n; idx++){
        target[idx] = source[idx];
    }
}

void copy_ptr(double * target,const double * source, int n){
    int idx;
    for (idx = 0; idx < n; idx++){
        *(target + idx) = *(source + idx);
    }
}

void copy_ptrs(double * target, double * source, const double * ar_end){
    int count = 0;
    double * ar_start = target;
    while(ar_start < ar_end){
        count++;
        *ar_start++ = *source++;
    }
    printf("copy_ptrs  %d 次\n", count);
}
编译运行:
copy_ptrs 19 次
0    0    0    0    0
1.08555e-3126.95139e-3106.95123e-3106.95139e-3100
1.1  2.2  3.3  4.4  5.5

Process finished with exit code 0

show_arr和show_ptr问题出在哪?
为什么copy_ptrs里面while循环了19次?
最佳答案
2022-10-11 16:54:27
本帖最后由 jhq999 于 2022-10-11 17:02 编辑
#include<stdio.h>

void show_arr(const double ar[], int n);

void copy_arr(double target[],const double source[], int n);
void copy_ptr(double * target,const double * source, int n);
void copy_ptrs(double * target, double * source, const double * ar_end);

int main(void){
    double source[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
    double target1[5];
    double target2[5];
    double target3[5];
    copy_arr(target1, source, 5);
    copy_ptr(target2, source, 5);
    copy_ptrs(target3, source, &source + 1);//////////&source + 1才是source的结尾
    show_arr(target1, 5);
    show_arr(target2, 5);
    show_arr(target3, 5);
    return 0;
}

void show_arr(const double ar[], int n)
{
    int i;

    for (i = 0; i < n; i++)
    {
        printf("%-5g", ar[i]);
    }
    putchar('\n');
}

void copy_arr(double target[],const double source[], int n){
    int idx;
    for (idx = 0; idx < n; idx++){
        target[idx] = source[idx];
    }
}

void copy_ptr(double * target,const double * source, int n){
    int idx;
    for (idx = 0; idx < n; idx++){
        *(target + idx) = *(source + idx);
    }
}

void copy_ptrs(double * target, double * source, const double * ar_end){
    int count = 0;
    double * ar_start = source;///////target3和source没有关系,两个数组,
    while(ar_start < ar_end){///////如果 ar_start= target,会有越界的情况,我这个。16次
        count++;
        *target++ = *ar_start++;////////
    }
    printf("copy_ptrs  %d 次\n", count);
}
copy_ptrs  5 次
1.1  2.2  3.3  4.4  5.5
1.1  2.2  3.3  4.4  5.5
1.1  2.2  3.3  4.4  5.5

Process returned 0 (0x0)   execution time : 0.237 s
Press any key to continue.
或者
copy_ptrs(target3, source, &target3+ 1);

void copy_ptrs(double * target, double * source, const double * ar_end){
    int count = 0;
    double * ar_start = target;
    while(ar_start < ar_end){
        count++;
        *ar_start++ = *source++;
    }
    printf("copy_ptrs  %d 次\n", count);
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-10-11 16:54:27 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jhq999 于 2022-10-11 17:02 编辑
#include<stdio.h>

void show_arr(const double ar[], int n);

void copy_arr(double target[],const double source[], int n);
void copy_ptr(double * target,const double * source, int n);
void copy_ptrs(double * target, double * source, const double * ar_end);

int main(void){
    double source[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
    double target1[5];
    double target2[5];
    double target3[5];
    copy_arr(target1, source, 5);
    copy_ptr(target2, source, 5);
    copy_ptrs(target3, source, &source + 1);//////////&source + 1才是source的结尾
    show_arr(target1, 5);
    show_arr(target2, 5);
    show_arr(target3, 5);
    return 0;
}

void show_arr(const double ar[], int n)
{
    int i;

    for (i = 0; i < n; i++)
    {
        printf("%-5g", ar[i]);
    }
    putchar('\n');
}

void copy_arr(double target[],const double source[], int n){
    int idx;
    for (idx = 0; idx < n; idx++){
        target[idx] = source[idx];
    }
}

void copy_ptr(double * target,const double * source, int n){
    int idx;
    for (idx = 0; idx < n; idx++){
        *(target + idx) = *(source + idx);
    }
}

void copy_ptrs(double * target, double * source, const double * ar_end){
    int count = 0;
    double * ar_start = source;///////target3和source没有关系,两个数组,
    while(ar_start < ar_end){///////如果 ar_start= target,会有越界的情况,我这个。16次
        count++;
        *target++ = *ar_start++;////////
    }
    printf("copy_ptrs  %d 次\n", count);
}
copy_ptrs  5 次
1.1  2.2  3.3  4.4  5.5
1.1  2.2  3.3  4.4  5.5
1.1  2.2  3.3  4.4  5.5

Process returned 0 (0x0)   execution time : 0.237 s
Press any key to continue.
或者
copy_ptrs(target3, source, &target3+ 1);

void copy_ptrs(double * target, double * source, const double * ar_end){
    int count = 0;
    double * ar_start = target;
    while(ar_start < ar_end){
        count++;
        *ar_start++ = *source++;
    }
    printf("copy_ptrs  %d 次\n", count);
}

评分

参与人数 1鱼币 +5 收起 理由
须弥芥子 + 5 鱼C有你更精彩^_^

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2022-10-11 17:19:32 | 显示全部楼层

非常感谢!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-8 01:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表