C language pointers and two-dimensional array _c language

Source: Internet
Author: User

Two-dimensional arrays are conceptually two-dimensional, with rows and columns, but in memory all array elements are contiguous, and there is no "gap" between them. Take the following two-dimensional array A as an example:

int a[3][4] = {0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}};

Conceptually, the distribution of a is like a matrix:

0 1 2 3
4 5 6 7
8 9 10 11

In memory, however, the distribution of a is one-dimensional and linear, and the entire array occupies a contiguous amount of memory:

The two-dimensional arrays in C language are arranged in rows, that is, to store the a[0] row, then store the a[1] line, then store the a[2] line, and the 4 elements in each row are stored sequentially. Array A is the int type, each element occupies 4 bytes, and the entire array occupies 4x (3x4) = 48 bytes.

The C language allows a two-dimensional array to be decomposed into multiple one-dimensional arrays to be processed. For array A, it can be decomposed into three one-dimensional arrays, i.e. a[0], a[1], a[2]. Each one-dimensional array contains 4 elements, such as a[0] containing a[0][0, a[0][1], a[0][2, a[0][3].

Assuming the address of the No. 0 element in array A is 1000, the first address of each one-dimensional array is shown in the following illustration:

To better understand the relationship between a pointer and a two-dimensional array, we first define a pointer variable p that points to a:

int (*p) [4] = A;

The * in parentheses indicates that P is a pointer to an array, and the type of the array is int [4], which is exactly the type of each one-dimensional array that a contains.

[] has a higher priority than *, () is necessary to add, if the naked writing int *p[4], then should be understood as int * (P[4]), p becomes an array of pointers, rather than a two-dimensional array pointer, which is already mentioned in the "C language pointer array."

When you add (subtract) a pointer to an operation, its forward (backward) step is related to the data type it points to, and the data type that P points to is int [4], then the p+1 forward 4x4 = 16 bytes, and the P-1 back 16 bytes, which is exactly the length of each one-dimensional array contained in array A. That is, p+1 causes the pointer to point to the next row of a two-dimensional array, and p-1 causes the pointer to point to the previous row of the array.

The array name A is also converted to the P-equivalent pointer in the expression!

Let's explore how to use the pointer p to access each element in a two-dimensional array. According to the above definition:

1) P points to the beginning of array A, or line No. 0; p+1 forward line, pointing to line 1th.

2) * (p+1) means to take the data on the address, that is, the entire 1th row of data. Note that one row of data, multiple data, not the No. 0 element in line 1th, the following results are a powerful proof of this:

#include <stdio.h>
int main () {
 int a[3][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, n}};
 int (*p) [4] = A;
 printf ("%d\n", sizeof (* (p+1)));
 return 0;

Run Result:


3) * (p+1) +1 represents the address of the 1th element in line 1th. How do you understand it?

* (p+1) represents the 1th row of data when used alone, and is converted to the first address of the 1th row of data in an expression. That is, the address of the No. 0 element in line 1th because using the entire row of data has no actual meaning, and the compiler encounters this situation as a pointer to the NO. 0 element of the row; like the name of a one-dimensional array, The entire array is represented when defined or used with sizeof, &, and is converted to a pointer to the NO. 0 element of the array when it appears in an expression.

4) * (* (p+1) +1) represents the value of the 1th element of line 1th. Obviously, adding a * represents the data on the address.

According to the above conclusion, it is easy to introduce the following equivalence relation:

A+i = = P+i
A[i] = = P[i] = = * (a+i) = = * (p+i)
A[I][J] = = P[i][j] = = * (a[i]+j) = = * (p[i]+j) = = * (* (a+i) +j) = = * (* (p+i) +j)

Instances use pointers to traverse two-dimensional arrays.

#include <stdio.h>
int main () {
 int a[3][4]={0,1,2,3,4,5,6,7,8,9,10,11};
 int (*p) [4];
 int i,j;
 For (i=0 i<3; i++) {
  for (j=0; j<4; j + +) printf ("%2d", * (* (p+i) +j));
  printf ("\ n");
 return 0;

Run Result:

0 1 2 3
4 5 6 7
8 9 10 11

The difference between a pointer array and a two-dimensional array pointer

Pointer arrays and two-dimensional array pointers are very similar when they are defined, except that the parentheses are in different positions:

int * (p1[5]); An array of pointers, which can be removed directly by writing int *p1[5];
int (*P2) [5]; Two-dimensional array pointer, cannot remove parentheses

The pointer array and the two-dimensional array pointer are essentially different: The pointer array is an array, except that each element holds a pointer, taking the above P1 as an example, which occupies 4x5 = 20 bytes of memory in a 32-bit environment. A two-dimensional array pointer is a pointer that points to a two-dimensional array, taking the above P2 as an example, which consumes 4 bytes of memory.

The above is the C language pointer and two-dimensional array of data collation, follow-up continue to supplement the relevant knowledge, thank you for your support of this site!

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.