Chapter 5 pointer to an array

Source: Internet
Author: User

When we talk about chapter 5, the two words of array are inseparable from us, and there are so many contents in the array. On the other hand, the relationship between arrays and pointers is indeed very close.

Generally, for the two-dimensional array int A [8] [9], we can define a pointer to it as follows:

INT (* P) [9];

The declaration form is similar to the int * P form that people are familiar with. Beginners are usually confused and generally have four points that they do not understand:

1. Why is it declared in this form?

2. (* P) How should I understand it?

3. Why must we explicitly declare the second dimension?

4. Why ignore the first dimension?

Next we will discuss these four questions one by one:

1. This form is defined in the statement syntax of the C standard. Since this chapter does not explain the standard, it only applies the standard. Therefore, I try to explain this statement in a concise way, A detailed discussion will be conducted in Chapter 7. The C standard statement contains two parts:

Statement:

Declaration specifier initialize declaration Operator table OPT (OPT means optional)

There is a type specifier in the Declaration specifier, and Int Is the type specifier. One of the forms in the initialization declaration table is:

Direct declaration Operator [constant expression opt]

(* P) [9] is the form of direct declaration plus.

2. * On the left of P is not a value operator, but a declaration, which indicates that p is a pointer. () Brackets cannot be removed. If [] OPERATOR priority is higher than *, P will first combine with [], so P becomes a pointer array, instead of pointing to the array pointer.

Digress:

* Another use of P is that * When * is a value operator, * P is a left value that represents a variable. Why * P is a variable? Some people may say that, because int I, * P = & I, this is actually not the reason. Strictly speaking, I is only a variable name, not a variable. In the symbol table of the compiler, the variable name is a symbolic address, the address value it represents is the address of the memory unit it points to. The actual variable is the memory unit, and friends who know the Assembly can easily distinguish it out. In the assembly, A variable name can be defined as follows:

Varw DW 10, 20

Varw is a variable name. It is an address in the Assembly, representing the memory unit of 10. Since P is initialized as & I, * P points to the memory unit represented by I, so * P is a variable. I is a common term for variables.

3. When defining a pointer, you must first define the pointer type. Because this is a pointer to an array, if the element type of the array is determined, then the pointer type is fixed. As mentioned above, the multi-dimensional array in C language is essentially an array nested, so the elements pointing to the array must have an array type. That is to say, the elements of this array are an array with 6 int elements, therefore, when defining P, you must specify the upper bound of the second dimension so that the P type can be set.

4. People who have such doubts have made a mistake and have not clearly identified what is a pointer and what is an array. the pointer P is viewed in the array thinking mode. When defining an array (non-static), you need to allocate a piece of memory statically in the stack, so you need to know the size of the memory. Therefore, when defining an array, You need to determine the upper bound of each dimension. Here, we only define a pointer. For a pointer definition, we need to know the type of the object it points to, and do not need to know the object size. This is redundant. Therefore, the first dimension of all pointers to the array is ignored.

The preceding section describes how to declare a pointer to a two-dimensional array. Similarly, you can use the same method to declare a pointer to an n-dimensional array, as shown below:

INT (* P) [X2] [X3] ...... [XN];

The first dimension can also be ignored, while the upper bound must be specified for other dimensions.

Finally, we will discuss a common misunderstanding of multi-dimensional arrays. Some people often think that two-dimensional arrays are second-level pointers, A second-level pointer int ** P can be used in the form of P [I] [J. First, it is wrong to call an array as a pointer. In the first chapter, the author has explained that the array name is an address and cannot be understood as a pointer. Second, it cannot be used in the form of P [I] [J], so P is a two-dimensional array. The C standard specifies the array reference, when array reference is not specified, the left side of the [] operator must be an array name, but an expression. Third, this is a "coincidence". In the final analysis, the Array Implementation of C language is the nesting of arrays, while the c Standard converts the [] operator to a similar * (a + I) + J) The two value operators "exactly" can be used for a second-level pointer. Fourth, P and P [I] do not have array types. The results of sizeof (P) and sizeof (P [I]) are only 4 bytes in size of a pointer. For a real array, both P and P [I] Have an array address.

In fact, int ** P is just a pointer to a one-dimensional pointer array, rather than a pointer to a two-dimensional array. Similarly, for n-level pointers, they can all be seen as a pointer to a one-dimensional pointer array. The elements of this pointer array are all n-1-level pointers.

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.