C + + two-dimensional arrays and pointers

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/carekee/articles/1948326.html

In summary, the pointer is actually the first address of a mutable array, said to be a mutable array, is
Variable of the amount of content contained in it, and is dynamically applied and released in order to charge
Save valuable memory resources. I've always liked a one-dimensional array, unless I had to.
The same is not the two-dimensional array, multi-dimensional is more rarely involved. Because one dimension is simple, the capacity
Is easy to understand, and the multidimensional array pointed to by the pointer is quite complex and therefore more
Have a need for discussion.
In this case, I'm going to talk about the comparison of three two-dimensional arrays:
(1), int **ptr;
(2), int *ptr[5];
(3), int (*ptr) [5];
The above three cases are two-dimensional arrays of integers, which can be used in the form of ptr[1 [1]
Way to access its content, but the difference is huge. Below I have four ways to
To discuss:
First, the content:
They are pointers in themselves, and their final content is integers. Look, I'm talking here.
Is the final content, not the middle content, like you write ptr[0], for three,
Its contents are an integer pointer, that is, an int *;ptr[1] [1] such a form
is its final content.
Second, the meaning:
(1), int **ptr represents a pointer to a "bunch" of pointers to integers.
(2), int *ptr[5] Represents a pointer to 5 pointers to integers.
(3), int (*ptr) [5] means pointing to a group of 5 integers
Pointer to a pointer to a group.
Three, the occupied space:
(1), int **ptr and (3), int (*ptr) [5], in 32-bit flat
The station, is 4 bytes, that is, a pointer.
But (2), int *ptr[5] is different, it is 5 pointers, it accounts for 5 * 4 = 20
bytes of memory space.
Iv. usage:
(1), int **ptr
Because it is a pointer to pointers, two memory allocations are required to use their final content. First
First, PTR = (int * *) New int *[5]; When this is allocated, it and (2)
The meaning is the same, and then the 5 pointers are allocated in memory, for example:
ptr[0] = new int[20];
It represents a 20 integer assigned to the NO. 0 pointer, and when assigned, ptr[0] is referred to as
To an array of 20 integers. You can use subscript usage ptr[0 [0] to
ptr[0] [19].
If there is no memory allocation for the first time, the PTR is a "wild" pointer and cannot be used
, if there is no second memory allocation, then ptr[0] and so on is also a "wild" pointer, also
It's not going to work. Of course, it is permissible to use it to point to an already defined address, which is another
(similar to the practice of "borrowing chickens to give birth to eggs"), this is not discussed here (hereinafter).
(2), int *ptr[5]
With this definition, the compiler has allocated 5 pointers to the space, which is quite
Memory allocation for the first time in (1). According to the discussion of (1), it is clear that a
Memory-Allocated. Otherwise it is the "wild" pointer.
(3), int (*ptr) [5]
I find this definition difficult to understand, not to understand, but to feel particularly labored,
Maybe I'm not used to such a definition. How do you describe it? It means "a bunch of"
pointers, each pointing to an array of 5 integers. If you want to assign a K-pointer,
This writes: PTR = (int (*) [5]) New int[sizeof (int) * 5 * k].
This is a one-time memory allocation. When assigned, PTR points to a contiguous address space,
where ptr[0] points to the first address of an array of No. 0 5 integers, ptr[1] points to the
The first address of an array of 5 integers.
To sum up, I think we can understand them this way:
int * * PTR <==> int ptr[x [y];
int *ptr[5] <==> int ptr[5 [x];
int (*PTR) [5] <==> int ptr[x [5];
Here x and Y represent a number of meanings.

Operation of a two-dimensional array pointer:

voidTest (int* * arr,intMintN) {}voidTest1 (int* Arr,intMintN) {}int_tmain (intARGC, _tchar*argv[]) {    intarr[3][3] =    {        { 1,2,3},//0,1,2{ A, A, +},//3,4,5{ -, at, -}//6,7,8    }; Test (arr,1,1); Test1 (arr,1,1);    GetChar (); return 0;}

This is a mistake, and when you see the error message, you have noticed

In fact, when the two-dimensional array is passed to the function, it is arr (*) [3], so it needs to be cast. You can refer to the above rules for the corresponding operation.

C + + two-dimensional arrays and 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.