※ 引述《dendrobium (石斛蘭)》之銘言:
: ※ 引述《Arim (Arim5566)》之銘言:
: : 小弟愚昧,請問一下
: : int dis[20][20]; //這是一個二維陣列,dis指向一個二維陣列
: : int **p;
: : 一般做動態陣列的時候,可以使p指向一個二維的陣列
: : 現在我不做動態配置,直接利用assignment: p=dis;
: : 將dis的位址assign給 p,可是compile不會給我過
: : 如果同樣的問題用一維的陣列就可以過
: : 請問這是為什麼呢??
: 簡單的來說, 這是因為 type 不同所導致的
: 20有點長 , 用 int dis[2][2]; 來說好了
: 他的記憶體配置是這樣的連續分佈
: ┌───┐
: dis│ │(type: int (*)[2] )
: └─┼─┘
: │
: ↓
: [0][0] [0][1] [1][0] [1][1]
: ─┬───┬───┬───┬───┬───┬───┬─
: .. │ │ │ │ │ │ │ ...
: ─┴───┴───┴───┴───┴───┴───┴─
: (type: int)
: 而如果是 int **p; 的話, 則是像下圖那樣
: [0] [1]
: ┌─┐ ┌──┬──┐ (type: int*)
: p │ ┼──→ │ │ │
: └─┘ └─┼┴─┼┘ [0] [1]
: (type: int**) │ │ ┌───┬───┐
: ╰──┼────→│ │ │ (type: int)
: │ └───┴───┘
: │ [0] [1]
: │ ┌───┬───┐
: ╰────→│ │ │ (type: int)
: └───┴───┘
: 這三塊記憶體都不一定是連續的
: 所以雖然兩個都是二維陣列
: 但是其實記憶體配置方式差滿多的
orz...原來就是出在加法上,害我傻了那麼久QQ。 (角落劃圈圈中...)
提供一下測試結果:
x[0][0] = 11
x[0][1] = 21
x[1][0] = 12
x[1][1] = 22
示意圖:
11 21
12 22
x = 0x22ff60
&x = 0x22ff60
*x = 0x22ff60
x[0] = 0x22ff60
&x[0] = 0x22ff60
*x[0] = 11
x[0][0] = 11
&x[0][0] = 0x22ff60
x[0][1] = 21
&x[0][1] = 0x22ff64
x[1] = 0x22ff68
&x[1] = 0x22ff68
*x[1] = 12
x[1][0] = 12
&x[1][0] = 0x22ff68
x[1][1] = 22
&x[1][1] = 0x22ff6c
x + 0 = 0x22ff60
*(x+0) = 0x22ff60
**(x+0) = 11
x + 1 = 0x22ff68
*(x+1) = 0x22ff68
**(x+1) = 12
x + 2 = 0x22ff70
*(x+2) = 0x22ff70
**(x+2) = 59
x + 3 = 0x22ff78
*(x+3) = 0x22ff78
**(x+3) = 2293680
*x + 0 = 0x22ff60
*x+0 = 0x22ff60
*(*x+0) = 11
*x + 1 = 0x22ff64
*x+1 = 0x22ff64
*(*x+1) = 21
*x + 2 = 0x22ff68
*x+2 = 0x22ff68
*(*x+2) = 12
*x + 3 = 0x22ff6c
*x+3 = 0x22ff6c
*(*x+3) = 22
請按任意鍵繼續 . . .
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.164.83.22
忘了附上程式碼....
#include <iostream>
using namespace std;
int main()
{
int x[2][2];
for (int i=0; i<2; i++)
for (int j=0; j<2; j++)
x[j][i] = (i+1)*10 + (j+1);
for (int i=0; i<2; i++)
for (int j=0; j<2; j++)
cout << "x[" << i << "]"
<< "[" << j << "] = " << x[i][j] << endl;
cout << endl;
cout << "示意圖:" << endl;
for (int i=0; i<2; i++){
for (int j=0; j<2; j++)
cout << x[i][j] << "\t";
cout << endl;
}
cout << endl;
cout << "x = " << x << endl;
cout << "&x = " << &x << endl;
cout << "*x = " << *x << endl;
cout << endl;
for (int i=0; i<2; i++){
//cout << "i = " << i << ":" << endl;
cout << "x[" << i << "] = " << x[i] << endl;
cout << "&x[" << i << "] = " << &x[i] << endl;
cout << "*x[" << i << "] = " << *x[i] << endl;
cout << endl;
for (int j=0; j<2; j++){
// cout << "j = " << j << ":" << endl;
cout << "x["<< i << "][" << j << "] = " << x[i][j]
<< endl;
cout << "&x[" << i << "][" << j << "] = "
<< &x[i][j] << endl;
}
cout << endl;
}
for (int i = 0; i<4; i++)
cout << "x + " << i << " = " << x + i << endl
<< "\t" << "*(x+" << i << ") = " << *(x+i) << endl
<< "\t\t" << "**(x+" << i <<") = " << **(x+i) << endl;
cout << endl;
for (int i = 0; i<4; i++)
cout << "*x + " << i << " = " << *x + i << endl
<< "\t" << "*x+" << i << " = " << *x+i << endl
<< "\t\t" << "*(*x+" << i <<") = " << *(*x+i) << endl;
cout << endl;
system("pause");
return 0;
}
※ 編輯: s3748679 來自: 218.164.83.22 (02/15 19:22)