c 语言中的array,pointer使用

来源:百度知道 编辑:UC知道 时间:2024/06/17 22:05:02
Write a program that initialises an array of double and then copies the contents of the array into two other arrays. Declare all arrays in the main program.
To make the first copy, make a function, which uses the array notation (the square brackets []) to access the elements of the array.
To make the second copy, write a function that uses the pointer notation and pointer incrementing to access the elements of the arrays.
Have each function take as function arguments the name of the target array and the number of elements to be copied.
Here is an example showing how the functions should be called, given the following declarations:
double source[4] = {1,2.3,4.5,6.7};
double destination1[4];
double destination2[4];
copy_array(source, destination, 4);
copy_ptr(source, destination, 4);
澄金亦炫的答案运行不出阿~

看我给你的信息

void copy_array(const double src[],double dst[],int num)
{
if (num > 4)
{
printf("the max is 4!");
return;
}

for (int i = 0 ; i < num;i++)
{
dst[i] = src[i];
}
return;

}

void copy_ptr(const double src[],double dst[],int num)
{
if (num > 4)
{
printf("the max is 4!");
return;
}

for (int i = 0;i < num;i++)
{
*(dst+i) = *(src+i);
}
return;

}
int main()
{
double source[4] = {1,2.3,4.5,6.7};
double destination1[4] = {0,0,0,0};
double destination2[4] = {0,0,0,0};
copy_array(source,destination1,4);
copy_ptr(source,destination2,4);

printf("destination1[4] is ");
for (int i = 0;i <=3;i++)
{
printf("%lf ",destination1[i]);
}
printf("\n");