Arrays and pointers

Source: Internet
Author: User

For one-dimensional arrays, the behavior of pointers and arrays is very similar.

#include <iostream>voidTestonedim () {intA[] = {1,2,3,4,5}; CharB[] ="abcdef"; int* AP =A; Char* BP =b; Std::cout<<"AP:"<< AP <<Std::endl; Std::cout<<"A:"<< a <<Std::endl; Std::cout<<"BP:"<< BP <<Std::endl; Std::cout<<"B:"<< b <<Std::endl; Std::cout<<"Ap[0]:"<< ap[0] <<Std::endl; Std::cout<<"A[0]:"<< a[0] <<Std::endl; Std::cout<<"Bp[0]:"<< bp[0] <<Std::endl; Std::cout<<"B[0]:"<< b[0] <<Std::endl;}intMain () {Testonedim ();}

The output is:

ap:002ef718a:002ef718bp:abcdefb:abcdefap[01a[01bp[  0]: ab[0]: a

From the results, the behavior of one-dimensional arrays and pointers is consistent.

Second , for a two-dimensional array, the case and a one-dimensional array is somewhat different, such as two-dimensional array can not be directly assigned to pointers pointer; If you want to point a pointer to a two-dimensional array, you need to assign the array to a pointer to the array, and if you want to assign a two-dimensional array to a pointer, You can then assign the pointer to the pointer on the array pointer. The code is as follows:

#include <iostream>voidTesttwodim ();intMain () {Testtwodim ();}voidTesttwodim () {inta[][3] = {1,2,3,4,5}; int* b[] = {a[0], a[1]}; int(* AP) [3] =A; int* * BP =b; Std::cout<<"AP:"<< AP <<Std::endl; Std::cout<<"Ap[0]:"<< ap[0] <<Std::endl; Std::cout<<"Ap[0][0]:"<< ap[0][0] <<Std::endl; Std::cout<<"----------------"<<Std::endl;

Std::cout << "BP:" << BP << Std::endl;
Std::cout << "bp[0": "<< bp[0" << Std::endl;
Std::cout << "bp[0][0": "<< bp[0][0" << Std::endl;
Std::cout << "----------------" << Std::endl;

Std::cout << "A:" << a << Std::endl;
Std::cout << "a[0": "<< a[0" << Std::endl;
Std::cout << "a[0][0": "<< a[0][0" << Std::endl;

 }

The output is:

Ap:0042fa1c
Ap[0]: 0042fa1c
Ap[0][0]: 1
----------------
bp:0042fa0c
Bp[0]: 0042fa1c
Bp[0][0]: 1
----------------
A:0042fa1c
A[0]: 0042fa1c
A[0][0]: 1

From the result, the pointer to the array and the behavior of the two-dimensional array are consistent, the pointer and the two-dimensional array behavior is not exactly the same, but the value is already the same.

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.