Multi-dimensional array pointer Array

Source: Internet
Author: User

I have never understood the concept of array thoroughly before. I only think that the array name is a constant pointer. Its usage is similar to that of basic pointers. So when I try to use a second-level pointer to access a two-dimensional array, errors often occur. The following is a newly written error.Program:

# Include <stdio. h> Int  Main (){  Int Iarray [ 2 ] [ 3 ] = {{ 1 , 2 , 3 },{ 4 , 5 , 6  }}; Int ** Parray = NULL; parray = Iarray; printf (  "  Array [0] [0] = % d \ n  " , Parray [ 0 ] [ 0  ]); Printf (  "  Array [1] [2] = % d \ n  " , Parray [ 1 ] [ 2  ]); Return   0  ;} 

 


At the beginning, I analyzed it as follows: the original array is similar to the pointer, and the one-dimensional array corresponds to the one-dimensional pointer, so the two-dimensional array name should be similar to the two-dimensional pointer, so there is no error in the above program. The numbers 1 and 6 should be printed. However, when I compile and run the program, a segment error occurs, that is, I have accessed the address space that should not be accessed. Where is the error? How should we write the correct program?
To solve the problem, I had to re-understand the meaning of the array. After carefully reading some books, I found that arrays are not as simple as I thought: a set of variables identified by a constant pointer. An array can also be regarded as a complete variable type: it has a name, a size, and an address. The name is the same as its address. Because the array has a size, when sizeof is used to calculate the array name, the actual array size is calculated, not the pointer size.
Because of this, the pointer to the array is very different from the pointer to the pointer. The most obvious difference between the two is that the pointer is stepping. We know that when the pointer is performing the ++ operation, the actual address that spans depends on the Data Type pointed to by the pointer:For a 32-bit server, if it is directed to int data, the actual address that spans is 4, the pointer data, and the actual address that spans is 4, when pointing to the array type, the actual address that spans is the length of the array.
Now let's look back at the above error program. According to the computation rule of the subscript referencing symbol [], we know that parray [0] [0] is actually ** parray, iarray is actually only an array variable name, and its value is the starting address of the entire array (in fact, & iarray, iarray, the iarray [0] And & iarray values are the starting addresses of the number groups and are the values assigned by the compiler during compilation ). In fact* Parray is already the value of iarray [0] [0 ].And ** parray is used to access the data in the address space where the address is 1. A segment error occurs naturally.
In fact, you can use a pointer to access a two-dimensional array. For example, the following program:

 
 Int Main (){  Int Iarray [ 2 ] [ 3 ] = {{ 1 , 2 , 3 },{ 4 , 5 , 6  }};  Int * Parray = NULL; parray = Iarray; printf (  " Array [0] [0] = % d \ n  " ,* Parray); printf (  "  Array [1] [2] = % d \ n  " , * (Parray + 1 * 3 + 2  ));  Return   0  ;} 

 


The array itself is arranged consecutively in the address space. Based on the number of rows and the number of columns, we can use a level-1 pointer to easily traverse all data in a two-dimensional array by calculating the address offset of the access unit.
We can also try to use a pointer to an array to access members of a two-dimensional array. The following is an example program:

 Int  Main (){  Int Iarray [ 2 ] [ 3 ] = {{ 1 , 2 , 3 },{ 4 , 5 , 6 }};  Int (* Parray )[ 3 ] = NULL; parray = Iarray; printf (  "  Array [0] [0] = % d \ n  " , Parray [ 0 ] [ 0  ]); Printf (  "  Array [1] [2] = % d \ n  " , Parray [ 1 ] [ 2 ]);  Return   0  ;} 

 

A Brief Analysis of this program: we know that the direction of the [] operator is from left to right. parray [1] [2] is equivalent to (* (parray + 1 )) [2]. Since parray is an array pointer and the array length is 3, * (parray + 1) indicates the array iarray [1, parray [1] [2] is equivalent to iarray [1] [2].

If we have to use a second-level pointer to access a two-dimensional array, We have to borrowPointer Array(Pointer-type data is stored in the array). The example program is as follows:

 Int  Main (){  Int Iarray [ 2 ] [ 3 ] = {{ 1 , 2 ,3 },{ 4 , 5 , 6  }};  Int * Iparray [ 2 ] = {Iarray [ 0 ], Iarray [ 1  ]};  Int ** Parray = NULL; parray = Iparray; printf (  "  Array [0] [0] = % d \ n  " , Parray [ 0 ] [ 0  ]); Printf (  "  Array [1] [2] = % d \ n  " , Parray [ 1 ] [ 2  ]);  Return   0  ;} 

 

 


Because the second-level pointer has to jump twice, additional storage space for the first-level pointer is required in the middle. SoWe do not recommend that you use a second-level pointer to access a two-dimensional array.

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.