须弥芥子 发表于 2022-10-11 16:10:37

拷贝数组遇到的问题

拷贝数组:
#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 = {1.1, 2.2, 3.3, 4.4, 5.5};
    double target1;
    double target2;
    double target3;
    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);
    }
    putchar('\n');
}

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

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.12.23.34.45.5

Process finished with exit code 0

show_arr和show_ptr问题出在哪?
为什么copy_ptrs里面while循环了19次?

jhq999 发表于 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 = {1.1, 2.2, 3.3, 4.4, 5.5};
    double target1;
    double target2;
    double target3;
    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);
    }
    putchar('\n');
}

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

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_ptrs5 次
1.12.23.34.45.5
1.12.23.34.45.5
1.12.23.34.45.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);
}

须弥芥子 发表于 2022-10-11 17:19:32

jhq999 发表于 2022-10-11 16:54
或者

非常感谢!
页: [1]
查看完整版本: 拷贝数组遇到的问题