About pointer - two-dimensional array discussion

xiaoxiao2021-03-06  45

Recently, I got the article on the pointer of Arong (essence), I feel that the content is very born.

Move, discuss it is also very meticulous. So I can't help but want to talk a few words, and I will explore it.

Summary, the pointer is actually the first address of the variable group, saying is a variable group,

Refers to the variableity of the number of content, and is a dynamic application and release, so that

Save valuable memory resources. I have always likes the one-dimensional array, unless I have already, I am

It is even more very small to have a two-dimensional array. Because of a simple one or more

It is easy to understand, and the multidimensional array of pointers is quite complex, and therefore

It is necessary to discuss it.

Gossip less, here I discuss with three two-dimensional arrays:

(1), int ** PTR;

(2), int * PTR [5];

(3), int (* ptr) [5];

The above three cases are all two-dimensional arrays, which can be used as PTR [1] [1]

The way the content is accessed; however, their difference is very large. Below I am from four aspects

have a discussion:

First, the content:

They themselves are pointers, and their final content is integer. Note that I said here

The final content, not the intermediate content, such as you write PTR [0], for the three,

Its content is an integer pointer, ie int *; PTR [1] [1]

It is its final content.

Second, the meaning:

(1), int ** PTR represents a pointer to the pointer to the integer pointing to the "one group".

(2), INT * PTR [5] indicates a pointer to 5 pointers that point to integers.

(3), int (* ptr) [5] indicates that the "one group" points points to 5 intense numbers

Group of pointers of the group.

Third, the space occupied:

(1), int ** PTR and (3), int (* ptr) [5], at 32

The stage is 4 bytes, namely a pointer.

But (2), int * PTR [5] is different, it is 5 pointers, it accounts for 5 * 4 = 20

One byte memory space.

Fourth, usage:

(1), int ** PTR

Because it is a pointer to the pointer, it is necessary to use two memory allocation to use its final content. first

First, PTR = (int **) new int * [5]; this is allocated, it and (2)

The meaning is the same; then the 5 pointers are then used to allocate, for example:

PTR [0] = New INT [20];

It is expressed as the 0th pointer assigned 20 integers, and after allocation, PTR [0] is referred to.

Array to 20 integers. At this time, you can use the subscript PTR [0] [0] to

PTR [0] [19].

If there is no first memory allocation, the PTR is a "wild" pointer, can't use

If there is no second memory allocation, PTR [0], etc. is also a "wild" pointer.

It is not possible. Of course, use it to point to an address that has been defined, is allowed, that is another

Usage (similar to "Borrowing Chicken Eggs"), this is not discussed (below).

(2), int * PTR [5]

If this is defined, the compiler has assigned five pointers space for it, which is quite

The first memory allocation in (1). According to the discussion of (1), it is obvious to carry it once.

Memory allocation. Otherwise it is the "wild" pointer. (3), int (* ptr) [5]

I think this kind of definition is very puzzled, I don't understand, but I feel that I understand it.

Maybe I am not very accustomed to such a definition. How to describe it? Its meaning is "a group"

The pointer, each pointer is an array of 5 integers. If you want to assign K pointers,

Writing this: PTR = (int (*) [5]) New int [sizeof (int) * 5 * k].

This is a one-time memory allocation. After allocation, PTR points to a continuous address space.

Where PTR [0] points to the first address of the 0th 5 integer array, PTR [1] points to the first

1 first address of 5 integer arrays.

In summary, I think it can be understood this:

INT ** PTR <==> int PTR [x] [Y];

INT * PTR [5] <==> int PTR [5] [x];

INT (* PTR) [5] <==> int PTR [x] [5];

Here X and Y are a number of meaning.

转载请注明原文地址:https://www.9cbs.com/read-69325.html

New Post(0)