69,371
社区成员
题目:编写一个函数,把一个内含7个元素的数组中第3-第5个元素拷贝至内含3个元素的数组中。
#include <stdio.h>
#define SIZE_ONE 7
#define SIZE_TWO 3
void copy_fun(double array_ar[], double source_ar[]);
int main(void)
{
int index;
double source[SIZE_TWO];
double array[SIZE_ONE] = { 1.2, 2.3, 4.2, 7.9, 6.3, 9.3, 9.3 };
void copy_fun(array, source);
printf("source's value as following:\n");
for (index = 0; index < SIZE_TWO; index++)
printf("%.1f ", source[index]);
return 0;
}
void copy_fun(double array_ar[], double source_ar[])
{
int i, index;
for (index = 0, i = 2; index < SIZE- 4; index++, i++)
source_ar[index] = array_ar[i];
}
帮你改了两个地方。
#include <stdio.h>
#define SIZE_ONE 7
#define SIZE_TWO 3
void copy_fun(double array_ar[],double source_ar[]);
int main(void)
{
int index;
double source[SIZE_TWO];
double array[SIZE_ONE] = { 1.2, 2.3, 4.2, 7.9, 6.3, 9.3, 9.3 };
copy_fun(array,source);//删去void
printf("source's value as following:\n");
for (index = 0; index < SIZE_TWO; index++)
printf("%.1f ", source[index]);
return 0;
}
void copy_fun(double array_ar[],double source_ar[])
{
int i, index;
for (index = 0, i = 2; index < SIZE_ONE- 4; index++, i++)//SIZE改为SIZE_ONE
source_ar[index] = array_ar[i];
}
#include <stdio.h>
#define SRC_ARR_LENGTH 7
#define DST_ARR_LENGTH 3
void copy_fun(double src_ar[], double dst_ar[])
{
int j = 0;
for (int i = 0; i < SRC_ARR_LENGTH; i++)
{
if ((i>=2) && (i<=4))
{
dst_ar[j++] = src_ar[i];
}
}
}
int main()
{
double dstArray[DST_ARR_LENGTH] = {0, 0};
double srcArray[SRC_ARR_LENGTH] = { 1.2, 2.3, 4.2, 7.9, 6.3, 9.3, 9.3 };
copy_fun(srcArray, dstArray);
printf("source's value as following:\n");
for (int i = 0; i < DST_ARR_LENGTH; i++)
{
printf("%.1f ", dstArray[i]);
}
printf("\n");
return 0;
}
函数调用语句不能带前面的void,带着那玩意就被编译器理解为函数原型声明了。
另外,啥地方定义了SIZE了?你的函数里SIZE-4没法解释啊。
失效,什么阶段什么表现;编译时,照着提示的错误信息和错误所在行,去修改;若运行结果不对,调试,在复制语句上断点,看每次复制时的状况,有错误,看错哪里,改。