Intuitive understanding of pointers to one array and two-dimensional arrays in C language

Source: Internet
Author: User

One-dimensional arrays and pointers:
It is well understood that an array and pointers are:
One-dimensional array name:
For such a one-dimensional array: int a[5]; A as an array name is the first address of our array, and a is an address constant.
First of all, the relationship between constants and variables, for the variable, with a box to analogy is better, declare a variable to declare a box, such as we open up an apple type of box, to this variable assignment is to put the Apple box into a real apple, this is the variable assignment.
For arrays, it is a set of boxes of the same type, a set of Apple chests that can be put into different apples.
One-dimensional array space:
After the variable is declared, we operate the compiler, compile the C source file, C at compile time will give our variables to open the corresponding space, and for the array, open space is continuous. We call each space a storage unit, each space has its own number, just as every family in our real life has its own house, the system array space address is connected, and our array name is a one-dimensional array first address, is an address constant.
Since the array is occupied by contiguous storage units, and the first address of the array is the array name, we can use the pointer variable to remove the corresponding address.
Pointer variable, that is, this box is dedicated to store other box address. We can give the array the first address to our pointer variable. int a[7],*p; P=a;

Understanding of two-dimensional arrays and pointers:

For beginners to understand the two-dimensional arrays and pointers is very vague, or it is difficult to understand, in fact, we relate to life, all this will become clear and thorough.
We understand the idea of a one-dimensional array of two-dimensional arrays, for a one-dimensional array, each box is stored in a specific apple, two-dimensional array, is the one-dimensional array in the contents of the storage of a series of boxes, which constitutes a two-dimensional array.
It's like a train, a train with a lot of fire cars, a seat in each of the cars, and the number of our fire car and the number of each train seat is different. e.g. int a[3][4];  3 cars, each with four seats next to each other in a fire compartment. The number of the actual seat and the number of our train compartment This is what we want to distinguish between, our fire compartment number is a[0],a[1],a[2] (from the seat point of view)
From the outside, the address of each of our cars, a+0,a+1,a+2, A is equivalent to a line address, fire compartment for the address of the unit, a+1 is jumping to the first address of the next compartment, and a[0] is equivalent to the first address of the seat, A[0]+0 will jump to the No. 0 carriage No. 0 seat, a[0]+ 1 will jump to the 11th seat of the No. 0 carriage.

The pointer array points to the two-dimensional address (seat address, actual address):
Create an array of pointers to reference the elements in a two-dimensional array: int *p[3], a[3][2],i,j; From the various can see that p is an array name, in the definition of the system for him to open 3 consecutive storage units; Preceded by this, the * number indicates that the base type of the array is the pointer type of int. We can use the for loop to point the pointer in this array to the first address of each row of "seats":

This array can be interpreted as an array of "seat" address types.
And the following we define this array: int a[3][2], (*PRT) [2] is an array of row pointers (array of train compartments). This allows us to put the "address of the car" in our row pointer array variable.
This allows us to assign a row address constant to the row address variable p=a. p+1 equivalent to a+1, equivalent to a[1] when P points to the first address of an array, you can refer to P[i][j by the following form:
(1) * (P[I]+J)
(2) * (* (p+i) +j))
(3) (* (P+i)) [j]
(4) P[i][j]
P is a row pointer variable, and the two-dimensional array name A is a row address variable.
The above is the understanding of two-dimensional arrays and pointers.

Intuitive understanding of pointers to one array and two-dimensional arrays in C language

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: 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.