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>
void copy_arr(double * source, double * target1, int var);
void copy_ptr(double * source, double * target2, int var);
int main(void)
{
double source[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
double target1[5];
double target2[5];
printf("原始数组:\n");
for(int i = 0; i < 5; i++)
{
printf("source[i] = %.2lf\n", source[i]);
}
copy_arr(source, target1, 5);
copy_ptr(source, target2, 5);
return 0;
}
void copy_arr(double * source, double * target1, int var) //使用数组符号
{
printf("第一份拷贝数组:\n");
for(int i = 0; i < 5; i++)
{
target1[i] = source[i];
printf("target1[i] = %.2lf\n", target1[i]);
}
}
void copy_ptr(double * source, double * target2, int var) //使用指针
{
printf("第二份拷贝数组:\n");
for(int i = 0; i < 5; i++)
{
*(target2 + i) = *(source + i);
printf("target2[i] = %.2lf\n", *(target2 + i));
}
}