语义错误
程序代码:
#include<stdio.h> #define N 5 void copy_arr(double source[],double target1[],int a); void copy_ptr(double *source,double *target2,int b); int main(void) { double source[N]={1.1,2.2,3.3,4.4,5.5}; double target1[N]={0}; double target2[N]={0}; printf("2ù×÷Ç°:\n "); printf("source:\t%g\t%g\t%g\t%g\t%g\n",source[0],source[1],source[2],source[3],source[4]); printf("target1:\t%g\t%g\t%g\t%g\t%g\n",target1[0],target1[1],target1[2],target1[3],target1[4]); printf("target2:\t%g\t%g\t%g\t%g\t%g\n",target2[0],target2[1],target2[2],target2[3],target2[4]); copy_arr(source,target1,5); copy_ptr(source,target2,5); printf("2ù×÷oó:\n"); printf("source :\t%g\t%g\t%g\t%g\t%g\n",source[0],source[1],source[2],source[3],source[4]); printf("target1:\t%g\t%g\t%g\t%g\t%g\n",target1[0],target1[1],target1[2],target1[3],target1[4]); printf("target2:\t%g\t%g\t%g\t%g\t%g\n",target2[0],target2[1],target2[2],target2[3],target2[4]); return 0; } void copy_arr(double source[],double target1[],int a) { int i; for(i=0;i<a;i++) { source[i]=target1[i]; } } void copy_ptr(double *source,double *target2,int a) { int i; for(i=0;i<a;i++) { *(source+i)=*(target2+i); } }
正确显示如下:
Before operation:
source : 1.1 2.2 3.3 4.4 5.5
target1: 0 0 0 0 0
target2: 0 0 0 0 0
After operation:
source : 1.1 2.2 3.3 4.4 5.5
target1: 1.1 2.2 3.3 4.4 5.5
target2: 1.1 2.2 3.3 4.4 5.5