The difference between int*p[] and int (*p) []

Source: Internet
Author: User

To illustrate:

1)int* p[2] is an array of pointers to int , that is: P is an array of pointers containing two elements, and the pointer is to an int type.

You can use this to:

#include <iostream>

using namespace Std;

int main (int argc, char* argv[])

{

int* p[2];

int a[3] = {1, 2, 3};

int b[4] = {4, 5, 6, 7};

P[0] = A;

P[1] = b;

for (int i = 0; i < 3; i++)

cout << *p[0] + i;//cout << **p + i;

cout << Endl;

for (i = 0; i < 4; i++)

cout << *p[1] + i;//cout << **p + i;

return 0;

}

(2) for Int (*p) [2], it is equivalent to the use of a two-dimensional array, except that it is an array of n rows and 2 columns , which can be used:

#include <iostream>

using namespace Std;

void Main () {

int (*p) [2];

int b[3][2] = {{1, 2}, {3, 4}, {5, 6}};

p = b;

for (int i = 0; i < 3; i++) {

for (int j = 0; J < 2; j + +)//cout << p[i][j]; cout << * (* (p+i) +j);

cout << Endl;

}

}

Attention:

(1) To determine the number of rows, the number of columns is uncertain, that is, 2*n type.

(2) A pointer usage for an array of type n*2, the number of rows is indeterminate, and the number of columns is determined.

for (1) its equivalent form is as follows:

#include <iostream>

using namespace Std;

void Main () {

int** Array;

Array = new int* [2];

int a[3] = {1, 2, 3};

int b[4] = {4, 5, 6, 7};

Array[0] = A; *array = A;

ARRAY[1] = b; * (array+1) = b;

for (int i = 0; i < 3; i++) cout << array[0][i];//cout << *array[0] + i;

cout << Endl;

for (int j = 0; J < 4; j + +) cout << array[1][j];//cout << *array[1] + j;

}

In fact, the usage of this dynamic two-dimensional array is our common use

The difference between int*p[] and int (*p) []

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.