开发者社区 问答 正文

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
为什么数组复制后跟源数组不一样??
初学者表示百思不得其解额 :

展开
收起
a123456678 2016-03-20 09:38:14 1955 分享 版权
1 条回答
写回答
取消 提交回答
  • 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];

    2019-07-17 19:08:38
    赞同 展开评论
问答地址: