Two-dimensional array name and second-level pointer, two-dimensional array pointer

Source: Internet
Author: User

Two-dimensional array name and second-level pointer, two-dimensional array pointer
1. pointer

1.1 a pointer contains two aspects: a) address value; B) the data type pointed.

1.2 The dereference operator (dereference operator) will be based on the current address value of the pointer and the data type to which it points, access a continuous memory space (the size is determined by the Data Type pointed to by the pointer), convert the content of this space to the corresponding data type, and return the left value.

Sometimes, the values of the two pointers are the same, but the data types are different. The obtained values of the unreferenced pointers are also different. For example,

1 char str [] = {0, 1, 2, 3};/* initialize with ASCII code */2 3 char * pc = & str [0]; /* The pc points to str [0], that is, 0 */4 5 int * pi = (int *) pc;/* the "value" of the pointer is an address, 32 bits. */

In this case, both pc and pi point to str [0], but * The pc value is 0 (that is, the ASCII code value is 0), and * the pi value is 50462976. It may be easier to understand to write it as a hexadecimal system: 0x03020100 (4 Bytes: 3, 2, 1, 0 ). I think you already understand that the pointer pi points to the int type. Therefore, when resolving the reference, you need to access the four bytes of continuous space and convert it to int to return.

2. Array

2.1 array name and pointer

We usually think that the array name isPointer constant(For example, if int a [10]; then a is an int * const), this understanding is incomplete. The correct understanding is as follows:

As the right value (for example, the right side of the value assignment statement), the group name can be regarded as a pointer constant (automatically converted by the system); as the left value, for example, taking the address and sizeof, it cannot be regarded as a pointer.

Sizeof (an array) returns the array size * Each element occupies the number of nodes, while sizeof (a pointer) returns 4.

 

2.2 Two-dimensional array

In fact, whether it is a one-dimensional or multi-dimensional array, It is a linear continuous space in the memory, so in terms of memory level, it is actually only one-dimensional.

Int a [3] [4] = {0, 1, 2, 3, 4, 5, 6, 8, 9, 10, 11}; int ** p; p = (int **); /* if no forced type conversion is performed, an error is returned */

Note:

1) p is a second-level pointer, which is first a pointer pointing to an int *;

2) a is a two-dimensional array name. It is first a pointer pointing to an int array containing four elements;

It can be seen that the types of a and p are not the same. If you want to assign a value to p, you need to force type conversion.

3. Why cannot I pass the two-dimensional array name to the second-level pointer?

If we assign a value to p, p = (int **) a; since p is a second-level pointer, of course we can use this: ** p; what is the problem?

1) First, let's take a look at the value of p. p points to a [0] [0], that is, the address where the value of p is a [0] [0;

2) Let's take a look at the value of * p. p points to the int type *, which occupies 4 bytes. According to the preceding process of referencing the operator: Starting from the address pointed by p, obtains the content of four consecutive bytes. Obtain the value of formal a [0] [0], that is, 0.

3) let's take a look at the value of ** p. What is the error? Of course, an error is reported because you have accessed the space with the address 0, and you are not authorized to access the space.

 

In fact, this is an interview question from Huawei in a year. If you are interested, you can also define the type as a two-dimensional array of the char type to see what will happen.

4. Two-dimensional array and second-level pointer-related parameter matching

5. Now, the question is?

 

 

6. About two-dimensional arrays, such A [2] [3]

    The values of a and a [0] are the same. First, a [0] is a one-dimensional array (the data type of the array is an integer). It is also a pointer constant, 1: the value of this pointer is the address of the first element of the array. 2: the data type to which the pointer belongs is an integer. So a [0] = & a [0] [0]. A is also an array (the data type of this array is a one-dimensional array), a is also a pointer constant, 1: the value of this pointer is equal to the address of the first element of the array, the first element of the array is a [0] (a [0] itself is also a one-dimensional array). What is the address of a [0], and a [0] is the number, the data type of this number is a three-dimensional integer array. Therefore, the size of a [0] memory is 12 bytes, and the first address of the number is xxx. This first address is the first address of the first element a [0] [0] of the array (note: both a [0] And a [0] [0] are numbers, and their (first) addresses are equal. The difference is that their data types are different, a [0] the Data Type of this number is a one-dimensional array, a [0] [0] the Data Type of this number is an integer, and the data type is not equal to the size, and sometimes the size is equal), so the value of the pointer constant a = & a [0] [0]. 2: The data type pointed to by a is a one-dimensional array, so the memory of a + 1 moves 12 bytes (moving a data type pointed to by a is so large ).

An array is a pointer constant, and a pointer constant is an array. This is obtained from * (p + n) constant and p [n. In most cases, this is true. It is not equivalent only under sizeof () and & operator. In sizeof () and & (take address), the system regards a of int a [5] as a variable rather than a pointer constant ),Number of variables whose data type is a one-dimensional array). Obviously, the variable size is 20 bytes, and the address of the variable, that is, & a is the variable type of a (array) the address of the first element is & a = & a [0]. So & a is also legal. Now: int a [5], int * const p = a; then a and p are equivalent in most cases, only in sizeof () and get address & operator are different. Sizeof (a) = 20, sizeof (p) = 4. & A = & a [0], & p! = & P [0]. In this case, a is not a pointer constant, but a variable with a data type of one-dimensional array ..... This is also because the program does not save the size of the array

References: http://blog.csdn.net/wu_nan_nan/article/details/51741030

Http://blog.sina.com.cn/s/blog_5c6f793801019t3t.html

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.