本来想试一下数组拷贝问题,然后发现 copy 函数无法正常运行,是怎么回事?在 copy 函数运行完后会停顿一段时间,且打印数组不会显示。
# include<stdio.h>
void print(int *nlist, int length){
int i;
for (i = 0; i < length; i++)
printf("%d ", nlist[i]);
printf("\n");
}
int *copy(int *nlist, int length){
int i;
int *output;
for (i = 0; i < length; i++)
output[i] = nlist[i];
return output;
}
int main(){
int i;
int a[5] = {1, 2, 3, 4, 5};
int *b, *c;
b = a;
print(b, 5);
c = copy(a, 5);
print(c, 5);
return 0;
}