[C/C ++ and pointer] Deep parsing-pointer and array [excellent]

Source: Internet
Author: User

I. Examples

A two-dimensional array can be replaced by a pointer pointing to an array, while a pointer array can be replaced by a pointer pointing to a pointer.

# Include <iostream> using namespace STD; void main () {char * A [] = {"hello", "the", "world "}; // pointer array char ** Pa = A; // pointer to pointer PA ++; cout <* pA ;}

 

For an array with a determined type, the element type and the number of elements are determined; the array degrades to the corresponding pointer, And the element type remains consistent;

The so-called two-dimensional array is only an array, so the two-dimensional array cannot be degraded to a second-level pointer, but can only be degradedPointer to a one-dimensional array

Example:

# Include <iostream. h>
Void main ()

{

Int A [2] [3]; // two-dimensional array

Int ** P = A; // the two-dimensional array cannot be degraded to a pointer to the pointer.

}

Why is it wrong ?? (Please do not say that the array name is a pointer. I know why the second-level pointer cannot be used to point to a two-dimensional array)

Resolution: first of all, you are not clear about the concepts of arrays and pointers. You are not clear about the nature of arrays. This is the root cause of the problem.

Int X [5]; In this definition, we define an array X, which has five array elements. The element type is int.

The first question is, why is X? X is the array name, and X represents the first address of the first element of the array. Yes, X is indeed the name of the array, and X is indeed the address value of the first array element.

Note that X represents the value of the first element of the array.Equal address value,Different Types.

So what is the type of array X? Some people say it is int * type. The following statements are supported:

Int * P = x; // This sentence is correct.

Is the array X really int * type? No, because the following statement is incorrect:

Int A = 10; X = & A; // it is acceptable for int * type variables.

So if X is not an int *, we can guess whether X is of the int * const type? That is to say, X is a pointer whose address value cannot be changed. This sentence seems a bit correct. But please take a look at the following example:

Int X [5] = {0}; int A = sizeof (x); // What is the value of?

In fact, here the value of A is 5*4 = 20, which is the number of bytes occupied by the entire array. Isn't X of the int * const type, that is, X should be a pointer type and should be 4 bytes, why is the number of bytes occupied by the entire array when sizeof is output? For example, the result of sizeof (int *) is 4. Therefore, we can see that the X type is neither int * nor int * Const. I

What is X in int X [5];? Let's say X is an array. This array has five elements, and each element is of the int type. We have a rule for identifying data types, for example:

Int X; // The X type is int.

Int * X; // The X type is int *

Int ** X; // int Type X **

INT (* X) [10]; // The X type is int (*) [10], which is actually a pointer to an array.

INT (* X) (INT, INT); // The type of X is int (*) (INT, INT), which is actually a pointer to the function.

We can see what data type A symbol is.Remove the symbol itself,The rest is the symbol type..

According to this inference, the type of X in int X [5]; should be int [5]. We can see that this type is not int.

The value of X in int X [5]; can be set to int * P = x. Why? It can only be said thatImplicit type conversionFor int * type. Therefore, the value can be assigned here because the type conversion is performed.

See the following example:

Void function (int x [5])

{

Cout <sizeof (x) <Endl; // output 4

} Why does output 4 instead of 4*5? We can see that the above function parameter is actually of the int type *, not an array type,

So when we define a function, the following are equivalent to the above:

Void function (int x []) // The number of elements can be omitted {cout <sizeof (x) <Endl; // output 4 here}

Void function (int * X) // you can directly write pointer variables. {cout <sizeof (x) <Endl; // output 4 here}

Let's look at a similar problem:

Int X [5]; int ** P = & X; // Why is an error reported? Because the type does not match.

The p type is int **, but the & X type is not int **. The type of & X is actually int (*) [5], because it takes the address of X, that is, this address is the address of the array, it is not a pointer to the first element of the array (that is, a two-dimensional pointer), but the address of the entire array.

So we can change it to the following: int (* P) [5] = & X.

 

2. What is the difference between a pointer to an array and a pointer to an array element?

For example, int * P; we should note that the P type is int *, P occupies 4 bytes of space, and P points to the Data Type of Int. The data type pointed to by P occupies 4 bytes.

Therefore, for pointer variables, we need to understand that pointer variables occupy space and are of a type. Secondly, the space pointed to by pointer variables is of a type and a space.

Int * P; char * P1; For pointer variables, P and P1 are all placed with the address value. To put it bluntly, it is a value, which occupies 4 bytes of space, but their types are different. The address in P points to int type data, and P1 points to char type data, this is mainly reflected in the difference between P ++ and P1 ++ in the number of bytes they move in the memory. We assume that int occupies 4 bytes and char occupies 1 byte. So for P, four bytes are moved forward, and P1 moves one byte. This means that their types are different, resulting in different computing processes.
Int X [5];

INT (* P3) [5]; at this time, P3 points to array X, so how much does P3 ++ actually move forward? It can be calculated to move 4*5 bytes. That is, P3 points to an array, which is the entire array. Therefore, when P3 moves, it regards an array as a whole. Therefore, the distance from the entire array is moved forward. Before reading your questions, let's look at a similar problem:

Int A [2] [3]; int ** P = & A; // I cannot use & A to assign values here. No. The reason for this error is that the & A type is not 'int' ** type. Therefore, the data types are incompatible and therefore cannot be assigned values. At the same time, these two types cannot be converted to each other. So what type is &. We say that & A takes the address of the entire array, so & A naturally points to the pointer of the entire array.

INT (* P) [2] [3] = & A; in this case, the value assignment is correct.If we want to assign values directly to a, what variables should we define to accept it? First, we must understand that the address type represented by the array name is the pointer to the first element of the array,

For example, int A [10]; int * P = A; in fact, this is equivalent to int * P = & A [0. Because the pointer type pointing to a [0] is int. Then & A is the address of the array. Its type is the pointer to the array, rather than the pointer to the first element of the array. The whole is different and their types are different. INT (* P) [10] = &;

So here'sA and & A are definitely not the same thing, although their address values are essentially the sameBut theirDifferent types. It is determined that they represent different meanings..

So I just mentioned the following example:

Int A [2] [3];

INT (* P) [2] [3] = &;

We can define such a Variable P to accept the value of &.

So we need to accept what type of variable A should define. A [2] [3] is a two-dimensional array. It can be seen that A is an array with two elements: a [0], the values of these two elements a [0] And a [1] are an array with three elements. Now we can regard a [0] And a [1] As the array name, so a [0] [0] Is The 0th elements of array a [0. The relationship is as follows: a [0] ----> A [0] [0], a [0] [1], a [0] [2] a [1]
----> A [1] [0], a [1] [1], a [1] [2], then what is a? In fact, array A has two elements, A [0], a [1], then the value of A is the address of its first element, that is, & A [0. What type is this? We know that if we regard a [0] as a whole, for example, if we use a to replace a [0], then a [0], A [1] is equivalent to a [0] [0], a [0] [1]. In this case, A is an array of the int type. The & A type is actually int (* P) [3. So the following code is correct:

Int A [2] [3]; int (* P) [3] = A; // so for your problem, you can do this.

 

 

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.