C Primer Plus10-2

本文介绍了一个C语言程序,该程序演示了如何通过两种不同的方法将一个double类型的数组复制到另外两个数组中。一种方法使用数组索引进行复制,另一种方法则利用指针和指针算术来完成复制任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*2.编写一个程序,初始化一个 double 数组,然后把数组内容复制到另外两个数组(3 个数组都需
要在主程序中声明)。制作第一份拷贝的函数使用数组符号。制作第二份拷贝的函数使用指针符号,并使用
指针的增量操作。把目标数组名和要复制的元素数目做为参数传递给函数。也就是说,如果给定了下列声
明,函数调用应该如下面所示:
double source [5]={1.1, 2.2, 3.3, 4.4, 5.5};
double targetl[5];
double target2 [5];
copy_arr (source, target1, 5);
copy_ptr (source, target1,5);*/

#include<stdio.h>
#include<stdlib.h>

void copy_arr(double[], double[], int);
void copy_ptr(double*, double*, int);

int main()
 {
     double source[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
     double target1[5] = {0};
     double target2[5] = {0};

     printf("before: target1:%.1lf  %.1lf  %.1lf  %.1lf  %.1lf\n",target1[0], target1[1], target1[2], target1[3], target1[4]);
     printf("before: target2:%.1lf  %.1lf  %.1lf  %.1lf  %.1lf\n",target2[0], target2[1], target2[2], target2[3], target2[4]);

     copy_arr (source, target1, 5);
     copy_ptr (source, target2, 5);

     printf("\nnow: target1:%.1lf  %.1lf  %.1lf  %.1lf  %.1lf",target1[0], target1[1], target1[2], target1[3], target1[4]);
     printf("\nnow: target2:%.1lf  %.1lf  %.1lf  %.1lf  %.1lf\n",target2[0], target2[1], target2[2], target2[3], target2[4]);

     system("pause");
     return 0;
}

void copy_arr (double s[], double t1[], int n)
 {    
     int i;

     for (i = 0; i < n; i++)
         t1[i] = s[i];
}

void copy_ptr (double *s, double *t2, int n)
 {
      int i;

      for (i = 0; i < n; i++)
        *(t2 + i) =*(s + i);
 }

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值