鱼C论坛

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

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

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

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

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

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

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

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

  6. int main(void){
  7.     double source[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
  8.     double target1[5];
  9.     double target2[5];
  10.     double target3[5];
  11.     copy_arr(target1, source, 5);
  12.     copy_ptr(target2, source, 5);
  13.     copy_ptrs(target3, source, source + 1);
  14.     show_arr(target1, 5);
  15.     show_arr(target2, 5);
  16.     show_arr(target3, 5);
  17.     return 0;
  18. }

  19. void show_arr(const double ar[], int n)
  20. {
  21.     int i;

  22.     for (i = 0; i < n; i++)
  23.     {
  24.         printf("%-5g", ar[i]);
  25.     }
  26.     putchar('\n');
  27. }

  28. void copy_arr(double target[],const double source[], int n){
  29.     int idx;
  30.     for (idx = 0; idx < n; idx++){
  31.         target[idx] = source[idx];
  32.     }
  33. }

  34. void copy_ptr(double * target,const double * source, int n){
  35.     int idx;
  36.     for (idx = 0; idx < n; idx++){
  37.         *(target + idx) = *(source + idx);
  38.     }
  39. }

  40. void copy_ptrs(double * target, double * source, const double * ar_end){
  41.     int count = 0;
  42.     double * ar_start = target;
  43.     while(ar_start < ar_end){
  44.         count++;
  45.         *ar_start++ = *source++;
  46.     }
  47.     printf("copy_ptrs  %d 次\n", count);
  48. }
复制代码

编译运行:
  1. copy_ptrs 19 次
  2. 0    0    0    0    0
  3. 1.08555e-3126.95139e-3106.95123e-3106.95139e-3100
  4. 1.1  2.2  3.3  4.4  5.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 编辑
  1. #include<stdio.h>

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

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

  6. int main(void){
  7.     double source[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
  8.     double target1[5];
  9.     double target2[5];
  10.     double target3[5];
  11.     copy_arr(target1, source, 5);
  12.     copy_ptr(target2, source, 5);
  13.     copy_ptrs(target3, source, &source + 1);//////////&source + 1才是source的结尾
  14.     show_arr(target1, 5);
  15.     show_arr(target2, 5);
  16.     show_arr(target3, 5);
  17.     return 0;
  18. }

  19. void show_arr(const double ar[], int n)
  20. {
  21.     int i;

  22.     for (i = 0; i < n; i++)
  23.     {
  24.         printf("%-5g", ar[i]);
  25.     }
  26.     putchar('\n');
  27. }

  28. void copy_arr(double target[],const double source[], int n){
  29.     int idx;
  30.     for (idx = 0; idx < n; idx++){
  31.         target[idx] = source[idx];
  32.     }
  33. }

  34. void copy_ptr(double * target,const double * source, int n){
  35.     int idx;
  36.     for (idx = 0; idx < n; idx++){
  37.         *(target + idx) = *(source + idx);
  38.     }
  39. }

  40. void copy_ptrs(double * target, double * source, const double * ar_end){
  41.     int count = 0;
  42.     double * ar_start = source;///////target3和source没有关系,两个数组,
  43.     while(ar_start < ar_end){///////如果 ar_start= target,会有越界的情况,我这个。16次
  44.         count++;
  45.         *target++ = *ar_start++;////////
  46.     }
  47.     printf("copy_ptrs  %d 次\n", count);
  48. }
复制代码
  1. copy_ptrs  5 次
  2. 1.1  2.2  3.3  4.4  5.5
  3. 1.1  2.2  3.3  4.4  5.5
  4. 1.1  2.2  3.3  4.4  5.5

  5. Process returned 0 (0x0)   execution time : 0.237 s
  6. Press any key to continue.
复制代码

或者
  1. copy_ptrs(target3, source, &target3+ 1);

  2. void copy_ptrs(double * target, double * source, const double * ar_end){
  3.     int count = 0;
  4.     double * ar_start = target;
  5.     while(ar_start < ar_end){
  6.         count++;
  7.         *ar_start++ = *source++;
  8.     }
  9.     printf("copy_ptrs  %d 次\n", count);
  10. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

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

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

  6. int main(void){
  7.     double source[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
  8.     double target1[5];
  9.     double target2[5];
  10.     double target3[5];
  11.     copy_arr(target1, source, 5);
  12.     copy_ptr(target2, source, 5);
  13.     copy_ptrs(target3, source, &source + 1);//////////&source + 1才是source的结尾
  14.     show_arr(target1, 5);
  15.     show_arr(target2, 5);
  16.     show_arr(target3, 5);
  17.     return 0;
  18. }

  19. void show_arr(const double ar[], int n)
  20. {
  21.     int i;

  22.     for (i = 0; i < n; i++)
  23.     {
  24.         printf("%-5g", ar[i]);
  25.     }
  26.     putchar('\n');
  27. }

  28. void copy_arr(double target[],const double source[], int n){
  29.     int idx;
  30.     for (idx = 0; idx < n; idx++){
  31.         target[idx] = source[idx];
  32.     }
  33. }

  34. void copy_ptr(double * target,const double * source, int n){
  35.     int idx;
  36.     for (idx = 0; idx < n; idx++){
  37.         *(target + idx) = *(source + idx);
  38.     }
  39. }

  40. void copy_ptrs(double * target, double * source, const double * ar_end){
  41.     int count = 0;
  42.     double * ar_start = source;///////target3和source没有关系,两个数组,
  43.     while(ar_start < ar_end){///////如果 ar_start= target,会有越界的情况,我这个。16次
  44.         count++;
  45.         *target++ = *ar_start++;////////
  46.     }
  47.     printf("copy_ptrs  %d 次\n", count);
  48. }
复制代码
  1. copy_ptrs  5 次
  2. 1.1  2.2  3.3  4.4  5.5
  3. 1.1  2.2  3.3  4.4  5.5
  4. 1.1  2.2  3.3  4.4  5.5

  5. Process returned 0 (0x0)   execution time : 0.237 s
  6. Press any key to continue.
复制代码

或者
  1. copy_ptrs(target3, source, &target3+ 1);

  2. void copy_ptrs(double * target, double * source, const double * ar_end){
  3.     int count = 0;
  4.     double * ar_start = target;
  5.     while(ar_start < ar_end){
  6.         count++;
  7.         *ar_start++ = *source++;
  8.     }
  9.     printf("copy_ptrs  %d 次\n", count);
  10. }
复制代码

评分

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

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

非常感谢!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-15 23:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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