Parameter passing with two-dimensional array names

Source: Internet
Author: User

Parameter passing with two-dimensional array names

Segmention Error occurs when two-dimensional array names are used as parameters. This is because it cannot properly address elements in a two-dimensional array. The correct method is as follows:

 

#include 
 
    #include 
  
       #define N   4  void testArray(int *a, int m, int n)  {      for(int i = 0; i < m; ++i)          for(int j = 0; j < n; ++j)          {              printf("a[%d][%d] = %d\n", i, j, *(a+i*n+j));          }  }    int main()  {      int a[2][N] = {{1, 2, 3, 4}, {5, 6, 7, 8}};        testArray((int *)a, 2, N);  }  
  
 


 

 

1. Pass the two dimensions of a two-dimensional array in the form of Variables

As follows:

 

[Cpp]View plaincopyprint?
  1. # Include
  2. # Include
  3.  
  4. # Define N 4
  5. Void testArray (int ** a, int m, int n)
  6. {
  7. For (int I = 0; I <m; ++ I)
  8. For (int j = 0; j <n; ++ j)
  9. {
  10. Printf ("a [% d] [% d] = % d \ n", I, j, * (int *) a + I * n + j ));
  11. }
  12. }
  13.  
  14. Int main ()
  15. {
  16. Int a [2] [N] = {1, 2, 3, 4}, {5, 6, 7, 8 }};
  17.  
  18. TestArray (int **) a, 2, N );
  19. }

     

     

    In this case, the sub-function cannot access the array elements in the form of a [I] [j], because the array elements are stored sequentially and the addresses are continuous, when an array element is accessed using a [I] [j], the specified element cannot be accessed sequentially. All elements can only be accessed by computing the specified element.

    2. Use a pointer variable pointing to a one-dimensional array, as shown in the following example:

     

    #include 
       
          #include 
        
             #define N   4  void testArray(int (*a)[4], int m, int n)  {      for(int i = 0; i < m; ++i)          for(int j = 0; j < n; ++j)          {              printf("a[%d][%d] = %d\n", i, j, *(*(a+i)+j));  //printf("a[%d][%d] = %d\n", i, j, a[i * n +j]);          }  }    int main()  {      int a[2][N] = {{1, 2, 3, 4}, {5, 6, 7, 8}};        testArray(a, 2, N);  }  
        
       

     

    Int (* a) [N] indicates the pointer variable pointing to the one-dimensional array. That is, the object that a points to is an array containing four integer elements. Note:

     

    Int * a [N] indicates that there is a one-dimensional array a [N]. All elements in this array are (int *) elements.

    Here, you can use a [I] [j] Or * (a + I) + j) to access elements in a two-dimensional array in a subfunction)

    In this case (* (a + I) [j], a [I * n + j]);, * (a + I) + j ), a [I] [j], * (int *) a + I * n + j) can be accessed.
     

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.