The Writing Method of pointer and array when passed to the function as parameters.

Source: Internet
Author: User

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 }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.