Pointer array and array pointer

Source: Internet
Author: User

Pointer array and array pointer
Pointer array and array pointer
1. Definition
Int * p [4]; // defines a pointer array p. p has four units, each of which is an int pointer.
Int (* p) [4]; // defines an array pointer p, which can point to an int array with four units
2. Usage of pointer Array
# Include

Int main ()
{
Int * p [4];
Int a = 1, B = 2, c = 3, d = 4;

P [0] = &;
P [1] = & B;
P [2] = & c;
P [3] = & d;
Printf ("% d \ n", * p [0], * p [1], * p [2], * (p [3]);

Return 0;
}
Program output: 1 2 3 4
Analysis: the usage of pointer arrays is relatively simple. Note that * p [0] And * (p [0]) are the same, in C, [] has a higher priority than the * operator.
3. array pointer usage
The usage of array pointers is complex and difficult to understand. We still need to combine some practical examples to understand them step by step.
Example 1:
Int main ()
{
Int a [8] = {1, 2, 3, 4, 5, 6, 7, 8 };
Int * p =;

Printf ("% d \ n", p [2]);

Return 0;
}
Program output result: 3
Analysis 1: This is a frequently used method for writing programs, but it hides some knowledge points that may not be noticed at ordinary times. Here p is an int type pointer, but in the program, p is similar to an array. How can this be understood? In fact, writing in this way is equivalent to using p as the base address [2] to indicate the offset relative to the base address. Here, offset two p points to the Type Unit, that is, the value of the third unit of a (Unit counting starts from 1) is 3. At the same time, remember that a has two types of understanding: the first one represents the general name of the eight int units, which is reflected in the & a. At this time, the & a type is int (*) [8]; The second understanding of the value of a represents a pointer of the int type, embodied in the [1] = * (a + 1) operation.

Example 2
# Include

Int main ()
{
Int a [8] = {1, 2, 3, 4, 5, 6, 7, 8 };
Int (* p) [8] = &;

Printf ("% p", p, a); // here the value of a is used, so a reflects the nature of the pointer.
Printf ("% d", (* p) [3]);
Printf ("% d \ n", * p [3]);

Return 0;
}
Program output: 0012FF60 0012FF60 4 1245120
Analysis 2: we can see that the output values of p and a are the same, but remember that the p and a types here are completely different, therefore, when assigning values, a is not directly assigned to p, but & a is used. Here, a is a pointer of the int type, and p is a pointer of the int (*) [8] type. If the two values are directly assigned to the compiler, an error is returned. The number of 3rd outputs is 4, that is, the value of a [3], but the number of the fourth output is obviously a wrong data. Remember that this number is not always used every time. How can this be understood? P is an array pointer, so * p represents a (although their values are the same). However, because [] has a higher priority than *, (* p) [3]) to obtain the value of a [3. However, p [3], according to the above understanding, is based on p, offset 3 p points to the unit, but p points to the array at this time, therefore, p points to an array of 8 int units. Therefore, p [3] indicates the next 4th arrays in the memory area (each array has 8 int units) * p [3] obtains the value of the first unit of the fourth array. Although this operation is out of bounds, the compiler cannot check the value.

Example 3: a more complex example
# Include

Int main ()
{
Int p [3] [4] [5];
Int * q = (int *) p;
Int (* s) [5] = & p [1] [0];
Int I, m;

For (I = 0; I <60; I ++)
Q [I] = I;

Printf ("% d", p [1] [7] [1]);
Printf ("% d", (* (s + 1) [3]);
Printf ("% d \ n", p [1] [7] [1] * (s + 1) [3]);

Return 0;
}
Program output: 56 40 2240
Analysis 3: This output may seem strange at first glance, but you can still get the correct result after careful analysis. First, let's look at the value assignment statement of the for loop. The p array has 3*4*5 = 60 units, and the for loop assigns the 60 units to 0 ~ respectively ~ 59. Let's look at p [1] [7] [1] again. This is obvious when the operation is out of bounds, but let's analyze the specific unit value: 1*20 + 7*5 + 1 = 56, which is equivalent to q [56]. This value is 56. What is hard to understand is (* (s + 1) [3]). However, according to the previous explanation, it is not difficult to understand this. s is an int (*) [5] type pointer, so s + 1 will point to the next array (each array contains five int units ), the value of s + 1 is equivalent to the value of & p [1] [1]. However, the [] operation is equivalent to offset the previous one as the base address, offset three units (each unit is an array), so it is equivalent to & p [1] [4]. However, since the second dimension of p has only three subscripts, so the more accurate statement is & p [2] [0], and then take the value of this address, that is, take the value of p [2] [0] [0, the value is 40.

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.