chinanoahli 2014-12-16 11:21 采纳率: 100%
浏览 1754
已采纳

C 复制数组的一点小问题 复制后输出结果和源数组不一样

 #include <stdio.h>

int i=5;

void input_array ( int array[] ){
    int a;
    for ( a=0; a<i; a++ ){
    scanf ( "%i", &array[a] );
    }
}

void output_array ( int array[] ){
    int a;
    for ( a=0; a<i; a++ ){
    printf ( "%i%s", array[a], "\t" );
    }
}

void copy_array ( int source[], int output[] ){
    int a;
    for ( a=0; a<i; a++ ){
        output[i]=source[i];
    }
}

int main(){
    int array[i];
    int copy_form_array [i];
    printf ("%s%i%s", "Pleast enter some numbers ( ", i, " integers limited ): \n");
    input_array ( array );
    output_array ( array );
    printf ( "%s", "\n" );
    copy_array ( array, copy_form_array );
    output_array ( copy_form_array );
    printf ("%s", "\n");
    return 0;
}

在做一个复制数组的练习的时候写了这些代码
编译时没有提示
运行结果如下

 $ ./20141210_am_copy_array 
Pleast enter some numbers ( 5 integers limited ): 
1 2 3 4 5
1   2   3   4   5   
1594599648  32767   1617756790  32767   1594599680

为什么数组复制后跟源数组不一样??
初学者表示百思不得其解额 :(

  • 写回答

4条回答 默认 最新

  • qqlindexi 2014-12-16 11:44
    关注
     void copy_array ( int source[], int output[] ){
        int a;
        for ( a=0; a<i; a++ ){
            output[i]=source[i];
        }
    }
    
    

    代换output[i]=source[i]; output[a]=source[a];

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置