淺析int*p[ ]與int(*p)[ ]的區(qū)別
更新時間:2013年07月11日 10:56:22 作者:
以下是對int*p[ ]與int(*p)[ ]的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以參考下
舉例說明:
1)int* p[2] 是一個指向int型的指針數(shù)組,即:p是包含兩個元素的指針數(shù)組,指針指向的是int型。
可以這樣來用:
<SPAN style="BACKGROUND-COLOR: rgb(255,255,255)">#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;
}</SPAN>
2)對于 int (*p)[2], 它相當(dāng)于一個二維數(shù)組的用法,只是它是一個n行2列的數(shù)組,可以這樣來用:
<SPAN style="BACKGROUND-COLOR: rgb(255,255,255)">#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;
}
}</SPAN>
注意:
(1)為行數(shù)確定、列數(shù)不確定,即為2*n型的。
(2)為n*2型的數(shù)組的指針用法,即行數(shù)不確定、列數(shù)確定。
對于(1)其等價形式如下:
<SPAN style="BACKGROUND-COLOR: rgb(255,255,255)">#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;
}</SPAN>
其實(shí)以上用法即這我們常用的動態(tài)二維數(shù)組的用法。
1)int* p[2] 是一個指向int型的指針數(shù)組,即:p是包含兩個元素的指針數(shù)組,指針指向的是int型。
可以這樣來用:
復(fù)制代碼 代碼如下:
<SPAN style="BACKGROUND-COLOR: rgb(255,255,255)">#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;
}</SPAN>
2)對于 int (*p)[2], 它相當(dāng)于一個二維數(shù)組的用法,只是它是一個n行2列的數(shù)組,可以這樣來用:
復(fù)制代碼 代碼如下:
<SPAN style="BACKGROUND-COLOR: rgb(255,255,255)">#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;
}
}</SPAN>
注意:
(1)為行數(shù)確定、列數(shù)不確定,即為2*n型的。
(2)為n*2型的數(shù)組的指針用法,即行數(shù)不確定、列數(shù)確定。
對于(1)其等價形式如下:
復(fù)制代碼 代碼如下:
<SPAN style="BACKGROUND-COLOR: rgb(255,255,255)">#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;
}</SPAN>
其實(shí)以上用法即這我們常用的動態(tài)二維數(shù)組的用法。
相關(guān)文章
c++實(shí)現(xiàn)加載so動態(tài)庫中的資源
下面小編就為大家?guī)硪黄猚++實(shí)現(xiàn)加載so動態(tài)庫中的資源。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12