Pass a multidimensional array to a function using a pointer form and reference and modify a multidimensional array using a pointer inside the function

Source: Internet
Author: User

Define a double two-dimensional array A and another equal-sized double-empty two-dimensional array B, and write a function that copies the data in A to B. and test the program.

1#include <stdio.h>2 voidCopy_ptr (Const Double(*s) [ A],Double(*tar) [ A],intRowintcol);3 intMainvoid){4     Const Doublerain[5][ A]={5{4.3,4.3,4.3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6},6{8.5,8.2,1.2,1.6,2.4,0.0,5.2,0.9,0.3,0.9,1.4,7.3},7{9.1,8.5,6.7,4.3,2.1,0.8,0.2,0.2,1.1,2.3,6.1,8.4},8{7.2,9.9,8.4,3.3,1.2,0.8,0.4,0.0,0.6,1.7,4.3,6.2},9{7.6,5.6,3.8,2.8,3.8,0.2,0.0,0.0,0.0,1.3,2.6,5.2}Ten},t1[5][ A]; OneCopy_ptr (RAIN,T1,5, A); A     //printf ("%.2f", T1[3][8]); -     return 0; - } the voidCopy_ptr (Const Double(*s) [ A],Double(*tar) [ A],intRowintCol) { -     inti,j; -      for(i=0; i<row;i++){ -          for(j=0; j<col;j++){ +* (* (tar+i) +j) =* (* (s+i) +j); -         } +     } A}

The two-dimensional array parameters in function prototypes and function definitions are

Double (*s) [+]

This means that the function accepts a pointer parameter that points to an array with 12 double values. That is, a sub-array.

Because the subarray itself is a pointer to each single double value, here the meaning of double (*s) [] is "s is a pointer, it points to (a step of 1 double, the range is a pointer of 0~11)", and the step of S is the total range of the pointer it points to.

In sum, the multidimensional array is actually behind the value, which implies a pointer tree. Or a pointer chain might be more accurate. Because the pin actually does not exist when an address is not pointed, the path from top to bottom is always unique at a point in time, so it is chained.

Pass a multidimensional array to a function using a pointer form and reference and modify a multidimensional array using a pointer inside the function

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.