请教大佬一下,为什么这个程序不能得出想要的结果?(我编写的这个函数好像失效了)

linux_dist 2023-03-22 16:37:24

题目:编写一个函数,把一个内含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];
}

...全文
84 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
民科小石头 2023-03-22
  • 打赏
  • 举报
回复

帮你改了两个地方。


#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];
}
不停的走着 2023-03-22
  • 打赏
  • 举报
回复

#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;
}
forever74 2023-03-22
  • 打赏
  • 举报
回复

函数调用语句不能带前面的void,带着那玩意就被编译器理解为函数原型声明了。
另外,啥地方定义了SIZE了?你的函数里SIZE-4没法解释啊。

linux_dist 2023-03-22
  • 举报
回复
@forever74 十分感谢,我已经对代码进行了修改,可以输出想要的结果了。
zara 2023-03-22
  • 打赏
  • 举报
回复

失效,什么阶段什么表现;编译时,照着提示的错误信息和错误所在行,去修改;若运行结果不对,调试,在复制语句上断点,看每次复制时的状况,有错误,看错哪里,改。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧