Two-dimensional array names and level two pointers

Source: Internet
Author: User

1. Pointers

1.1 A pointer contains two aspects: a) the address value; b) The data type that is pointed to.

The 1.2 dereference operator (dereference operator) accesses a contiguous amount of memory space (determined by the data type pointed to by the pointer) based on the current address value of the pointer and the data type pointed to, converting the contents of the space into the appropriate data type and returning an lvalue.

Sometimes, the values of two pointers are the same, but the data types are different, and the values that the dereference takes are different, for example,

1 CharStr[] ={0,1,2,3};/*initialization with ASCII code of characters*/  2   3 Char* PC = &str[0];/*pc pointing to str[0], i.e. 0*/  4   5 int* pi = (int*) PC;/*The "value" of the pointer is an address, 32 bits. */  

At this point, both the PC and Pi point to str[0], but the value of *PC is 0 (that is, the ASCII code value is 0 characters), and the value of *pi is 50462976. Perhaps it would be easier to interpret it as a hex: 0x03020100 (4 bytes, respectively, 3,2,1,0). I think you've got it. The pointer pi points to the type int, so when you dereference it, you need to access 4 bytes of contiguous space and convert it to an int return.

2. Arrays

2.1 Array names and pointers

Usually we think that the array name is a pointer constant (for example, int a[10]; Then a is an int * const), this understanding is not comprehensive, the correct understanding is as follows:

As an rvalue (for example, to the right of an assignment statement), the array name is visualized as a pointer constant (System auto-conversion), and as an lvalue, such as an address, sizeof, is not considered a pointer.

sizeof (an array) returns the array size * The number of bytes per element, and sizeof (a pointer) returns 4.

2.2 Two-dimensional arrays

In fact, whether it is a one-dimensional or multidimensional array, is a linear contiguous space in memory, so at the memory level, is actually only one dimension.

int a[3[4] = {0,1,2,3,4,5,6 ,7,8,9,ten,one};     int * * p    ; = (int* *) A;       /* */  

Description

1) p is a level two pointer, it is first a pointer, pointing to a int*;

2) A is a two-dimensional array name, which is first a pointer to an int array containing 4 elements;

Thus, the type of a and p is not the same, if you want to assign a to p, you need to force the type conversion.

3. Why can't I pass a two-dimensional array name to a level two pointer?

If we assign a value to P,p = (int**) A; Since P is a level two pointer, then of course it can be used: **p; What would be the problem?

1) First look at the value of P, p pointing to a[0][0], that is, the value of P is a[0][0] address;

2) Take a look at the value of *p, p is pointing to a type of int*, which is 4 bytes, according to the previous procedure of the dereference operator: Starting with the address that P points to, taking 4 bytes of content in a row. The value of the obtained formal a[0][0], i.e. 0.

3) Another look at the value of **p, eh, error? Of course the error, because you have access to the address of 0 space, and this space you do not have access to.

In fact, this is a year of Huawei's face test. It is also interesting to define the type of a as a two-dimensional array of type char and see what happens.

4. Parameter matching for two-d arrays and two-level pointers

5. So the question is, what's the answer?

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

    A and a[0], the first a[0] is a one-dimensional array (the data type of an array is an integer) and a pointer constant, 1: The value of the pointer is the address of the first element of the array, and 2: The data type that is pointed to is an integer. So a[0]=&a[0][0]. A, also the array (the data type is a one-dimensional array), A is also a pointer constant, 1: The value of the 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 a one-dimensional array), a[0] The address is, a[0] is the number, The data type of the number is an integer one-dimensional array of size 3. So a[0] This number, the memory size is 12 bytes, the number of the first address is XXX. The first address of the a[0] is the first element of the array a[0][0], (note: a[0] and a[0][0] are also numbers, and their (first) address is equal, the difference is their data type is different, a[0] This number of data type is a one-dimensional array, a[0][0] The data type of this number is an integer, the data type is not equal to the size also does not want to wait, also sometimes the size is equal), so a this pointer constant value a=&a[0][0]. The data type that 2:a points to is a one-dimensional array, so a+1 memory moves 12 bytes (the data type pointed to by moving a is so large).

An array is a pointer constant, and a pointer constant is a representation of an array. This is derived from the identity of * (P+n) and P[n]. In most cases it is right. Only the sizeof () and & operators are not equivalent at this time. In sizeof () and & (address), the system takes int a[5] A as a pointer constant but as a variable (number), and the data type is a variable (number) of one-dimensional array. Obviously the size of the variable is 20 bytes, and the address of the variable, that is, &a is the address of the first element of a variable type (array), which 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, except for sizeof () and the fetch address & operator. sizeof (a) =20,sizeof (p) = 4. &a=&a[0],&p!=&p[0]. At this point A is not a pointer constant but a variable with a data type of one-dimensional array ..... This is also because the size of the array is not saved in the program

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

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

Two-dimensional array names and level two 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.