Intuitive understanding C language pointer to an array and a two-dimensional array of pointers _c language

Source: Internet
Author: User

one-dimensional arrays and pointers:
It is well understood for an array and a pointer:
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 a variable, it's better to use a box analogy, declaring a variable declaring a box, such as we open up an apple type of box, give this variable value is to put the Apple box into a real apple, this is the variable assignment.
For an array, it's a group of identical boxes, a group of Apple boxes that can be put into different apples.
One-dimensional array space:
Variables are declared, we operate the compiler, our C source file to compile, C in the compilation will give our variables to open up 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 like our real life every family has its own house, the system array space address is connected, and our array name is the first address of one-dimensional array, is an address constant.
Pointer:
Since the array is a contiguous storage unit, 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 other box addresses. We can put the first address of the array to our pointer variable. int a[7],*p; P=a;

Two dimensional arrays and the understanding of pointers:

For beginners, the understanding of two-dimensional arrays and pointers is vague, or it is difficult to understand, but in fact we relate to life and it all becomes clear and thorough.
We understand two-dimensional arrays with the idea of understanding one-dimensional arrays, and for one-dimensional arrays, each box contains a specific apple, a two-dimensional array, which stores a series of boxes in the contents of a one-dimensional array, thus constituting a two-dimensional array.
It's like a train, a train with lots of fire compartments, and a seat in each compartment, so the number of the fire compartment and the number of each train seat is different. For example int a[3][4];  3 fire compartments, each with four seats in each compartment. The actual seat number and the number of our train compartment This is what we want to distinguish between, our compartment number is a[0],a[1],a[2] (from the seat point of view)
Look from the outside, the address of each of our fire carriages, a+0,a+1,a+2, A is equivalent to a line address, the fire compartment for the unit address, a+1 is to jump to the next compartment of the first address, and A[0] is equivalent to the first address of the seat, A[0]+0 will jump to the No. 0 compartment of the No. 0 seat, a[0]+ 1 will jump to the 11th seat in the No. 0 carriage.

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

The

This array can be understood to be an array of "seat" address types.
and the array we define below: int a[3][2], (*PRT) [2] is an array of row pointers (train compartment arrays). So we can put each section "compartment address" into 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, it can be referenced in the following form P[i][j]:
(1) * (P[I]+J)
(2) * (* (p+i) +J))
(3) (* (P+i)) [j]
(4) p[i][j]
P is a row pointer variable, and two-dimensional array name A is a row address variable.
The above is the understanding of two-dimensional arrays and pointers.
         

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.