这是一个用三个函数分别把source数组拷贝给三个target数组的代码,为什么现在这样运行target2打印出来是一堆随机的奇怪数字,但是把函数2和函数3的调用换一下位置,又运行正常了呢?
#include<stdio.h>
void copy_arr(double* t, double* s, int n);
void copy_ptr(double* t, double* s, int n);
void copy_ptrs(double* t, double* s, float* n);
int main(void)
{
const double source[5] = { 1.1,2.2,3.3,4.4,5.5 };
double target1[5];
double target2[5];
double target3[5];
int index;
copy_arr(target1, source, 5); //函数1
copy_ptr(target2, source, 5); //函数2
copy_ptrs(target3, source, source + 5); //函数3
printf("target1: ");
for (index = 0; index < 5; index++)
printf("%5g", target1[index]);
printf("\n\n");
printf("target2: ");
for (index = 0; index < 5; index++)
printf("%5g", target2[index]);
printf("\n\n");
printf("target3: ");
for (index = 0; index < 5; index++)
printf("%5g", target3[index]);
printf("\n\n");
return 0;
}
void copy_arr(double* t, double* s, int n)
{
int i;
for (i = 0; i < n; i++)
t[i] = s[i];
}
void copy_ptr(double* t, double* s, int n)
{
for (int i = 0; i < n; i++)
*(t + i) = *(s + i);
}
void copy_ptrs(double* t, double* s, float* n)
{
int x, i;
x = n - s;
for (i = 0; i < x; i++)
*(t + i) = *(s + i);
}

