#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
為什麼數組復制後跟源數組不一樣??
初學者表示百思不得其解額 :(
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];