C + + pointers and arrays

Source: Internet
Author: User

Working with multidimensional arrays inside C + +

    intarr[2][3] = { {1,2,3}, {4,5,6} };int* P1 = (int*) (arr +1);cout<< p1[0] << Endl;int* P2 = (int*) (&arr +1);cout<< p2[-1] << Endl;cout<< arr << Endl;cout<< &arr << Endl; P1 = (int*) arr[1];cout<< p1[0] << Endl;

Run the above code to debug intermediate process discovery
ARR is an int (*) [3] type, &arr is an int (*) [2][3] type, such as the following example:

    int arr[2][3123456 } };    int (*p1)[3] = arr;    int (*p2)[2][3] = &arr;

&arr returns the entire array as a pointer

And wrote a small example.

    int arr[2][3{ { 1, 2, 3 }{ 4, 5, 6 } };    cout << arr << endl << &arr << endl;

Output Result:

I understand that arr is the array name, the first address of the array store, &arr is the entire array as a variable, so the return address is the first address of the array, is the same, just pointer type, arr is the shaping pointer, &arr is an array pointer.

Array pointer: Pointer to an array with the same size as the array
Array of pointers: one element represents a pointer, the pointer size is the same, and is approximate by the pointer array type.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + pointers and arrays

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.