输出数组元素,输出结果错误
程序代码:
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ *初始化一个数组,将数组内容复制到另外两个数组中 *第一份拷贝使用数组下标,第二份拷贝使用指针增量操作,把目标数组名和要复制的元 *素数目作为参数传递给函数 *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ #include <stdio.h> #define SIZE 5 void copy_index(double source[],double target[],int arrLen); void copy_pointer(double *source,double *target,int arrLen); void print_array(double array[],int arrLen); int main(void) { double source[SIZE]={1.1,2.2,3.3,4.4,5.5}; double target1[SIZE]; double target2[SIZE]; copy_index(source,target1,SIZE); copy_pointer(source,target2,SIZE); print_array(target1,SIZE); print_array(target2,SIZE); return 0; } void copy_index(double source[],double target[],int arrLen) { int i; for(i=0;i<arrLen;i++) target[i]=source[i]; } void copy_pointer(double *source,double *target,int arrLen) { int i; for(i=0;i<arrLen;i++){ *target++=*source++; printf("By copy_pointer: Target[%d] = %.1f.\n",i,*target); } } void print_array(double array[],int arrLen) { int i; for(i=0;i<arrLen;i++) printf("By Pointer: Target[%d] = %.1f.\n",i,*array++); }
输出结果:
By copy_pointer: Target[0] = 0.0.
By copy_pointer: Target[1] = 5715102993279205789256153893914470542232112909384201926228276125698620488519110470997992189482283978356249466410566275192091273279460128355956028825434645840958782393875580439971946718728990551972880532157655305269667042594652160.0.
By copy_pointer: Target[2] = 0.0.
By copy_pointer: Target[3] = 0.0.
By copy_pointer: Target[4] = 0.0.
By Pointer: Target[0] = 1.1.
By Pointer: Target[1] = 2.2.
By Pointer: Target[2] = 3.3.
By Pointer: Target[3] = 4.4.
By Pointer: Target[4] = 5.5.
By Pointer: Target[0] = 1.1.
By Pointer: Target[1] = 2.2.
By Pointer: Target[2] = 3.3.
By Pointer: Target[3] = 4.4.
By Pointer: Target[4] = 5.5.
By copy_pointer: Target[1] = 5715102993279205789256153893914470542232112909384201926228276125698620488519110470997992189482283978356249466410566275192091273279460128355956028825434645840958782393875580439971946718728990551972880532157655305269667042594652160.0.
By copy_pointer: Target[2] = 0.0.
By copy_pointer: Target[3] = 0.0.
By copy_pointer: Target[4] = 0.0.
By Pointer: Target[0] = 1.1.
By Pointer: Target[1] = 2.2.
By Pointer: Target[2] = 3.3.
By Pointer: Target[3] = 4.4.
By Pointer: Target[4] = 5.5.
By Pointer: Target[0] = 1.1.
By Pointer: Target[1] = 2.2.
By Pointer: Target[2] = 3.3.
By Pointer: Target[3] = 4.4.
By Pointer: Target[4] = 5.5.
谢谢。