编写程序,初始化一个double类型的数组,然后将该数组内容拷贝至3个其他数组中。其中,目标数组1(target1[])使用数组表示法的函数,target2[]和target3[]使用指针。
#include<stdio.h>
void copy_arr(double target1[], double source[], int n);
void copy_ptr(double* target2, double* source, int n);
void copy_ptrs(double* target3, double* source, double* p);
void show(double target[]);
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];
double* p;
copy_arr(target1,source,5);
copy_ptr(target2,source,5);
copy_ptrs(target3, source, source+5);
show(target1);
show(target2);
show(target3);
return 0;
}
void copy_arr(double target1[], double source[], int n)
{
for (int i = 0;i < n;i++)
{
target1[i] = source[i];
}
}
void copy_ptr(double* target2, double* source, int n)
{
while (n) {
*target2 = *source;
target2++;
source++;
n--;
}
}
void copy_ptrs(double* target3, double* source, double* p)
{
while (source<p)
{
*target3 = *source;
target3++;
source++;
}
}
void show(double target[])
{
for (int i = 0;i < 5;i++) {
printf("%.1f\t", target[i]);
}
printf("\n");
}