The Writing Method of pointer and array when passed to the function as parameters.
Write a program, initialize a double array, and copy the array content to the other two arrays (the three arrays must be declared in the main program ). The function that creates the first copy uses the array symbol. The function that creates the second copy uses the pointer symbol and the incremental operation of the pointer. Pass the target array name and the number of elements to be copied as a parameter to the function. That is to say, if the following declaration is given, the function call should be shown below:
Double source [5] = {1.1, 2.2, 3.3, 4.4, 5.5 };
Double targetl [5];
Double target2 [5];
Copy_arr (source, target1, 5 );
Copy_ptr (source, target1, 5 );
1 #include <stdio.h> 2 3 void copy_arr(double s[],double tar[],int c); 4 void copy_ptr(double *s,double *tar,int c); 5 6 int main(void){ 7 double s[5]={1.1, 2.2, 3.3, 4.4, 5.5}; 8 double t1[5],t2[5]; 9 copy_arr(s,t1,5);10 copy_ptr(s,t2,5);11 int i;12 printf("t1\t\tt2\n");13 for(i=0;i<5;i++)14 {15 printf("t1[%d]=%.1f\t",i,t1[i]);16 printf("t2[%d]=%.1f\n",i,t2[i]);17 }18 return 0;19 }20 21 void copy_arr(double s[],double tar[],int c){22 int i;23 for(i=0;i<c;i++){24 tar[i]=s[i];25 }26 }27 28 void copy_ptr(double *s,double *tar,int c){29 int i;30 for(i=0;i<c;i++){31 *(tar+i)=*(s+i);32 }33 }
You can also use the incremental operator in the function definition section:
1 void copy_arr(double s[],double tar[],int c){2 int i=0;3 while(i<c)tar[i]=s[i++];4 }5 6 void copy_ptr(double *s,double *tar,int c){7 int i=0;8 while(i<c)*(tar+i)=*(s+i++);9 }