用函数写一个拷贝数组的程序
问题描述:
我写了一个程序之后发现打印出来的是一串随机数,不知道哪里出了问题,下面是代码
#include <stdio.h>
#include <stdlib.h>
int main()
{
void copy_array1(double a[],double b[]);
int copy_array2(double *p,double *q);
int copy_array3();
double source[5]={1.1,1.2,1.3,1.4,1.5};
double target1[5]={1,2,3,4,5};
copy_array1(source,target1);
printf("%d",target1);
system("pause");
}
void copy_array1(double source[],double target1[])
{
int i;
for(i=0;i<5;i++)
{
target1[i]=source[i];
}
}
答
首先,第一你输出的是一个double类型,应该使用 %lf 个数输出。
其次,你样输出数组元素,应该使用循环输出,而不是把数组的首地址放在printf 中。