C語言指標與二維陣列

2021-06-29 15:06:17 字數 3653 閱讀 1421

#includeint main(),,,};

int (*p)[3] = null;

p = array;  // p is same as array,the use of p is same as array.

//so p is the two-devision pointer same as array

int *p1 = &array[0][0]; //p1 is the one-devision pointer

int *p2 = array[0];

//int **p3 = array;  this is wrong. the array's kind is int(*)[3]

//but p3 is int**

printf("%d  ",*array[0]);//out:1

//(*p)[n]  think array is the one-devsion array

//so (*p) is the header pointer of the one-devision array

//so (*p)[1],(*p)[2]......(*p)[i] is right

printf("%d  ",(*p)[0]);//out:1

printf("%d  ",(*p)[1]);//out:2

printf("%d  ",(*p)[2]);//out:3

printf("(*p)[3]:%d  ",(*p)[3]);//out:4

printf("(*p)[4]:%d  ",(*p)[4]);//out:5

printf("(*p)[5]:%d  ",(*p)[5]);//out:6

//think p is same totaly as array

//*p pointing the zero row

//*(p+1) pointing the one row

//*(p+2) pointing the two row

//*(p+3) pointing the three row

//and array is four rows, so *(p+4) is overflow

printf("%d  ",(*(p+1))[0]);   //out:4

printf("%d  ",(*(p+2))[0]);   //out:7

printf("%d  ",*(p+3)[0]);   //out:10

printf("%d  ",*(p+3)[1]);   //overflow

printf("%d  ",(*(p+3))[1]);   //11

printf("%d  ",(*(array+3))[1]);//11

printf("%d  ",*(array+3)[1]);//overflow

printf("*(array+3)[0]: %d  ",*(array+3)[0]); //10

printf("*(array+3)[1]: %d \n ",*(array+3)[1]); //overflow

printf("%d  \n",*(*(p+3)+1));                   //out:11 

printf("*(array[3*3+1]):%d  \n",*(array[3*3+1])); //overflow

printf("*array[3*3+1]:%d  \n",*array[3*3+1]); //overflow

//printf("*array[3*3+1]:%d  ",**array[3*3+1]); // is wrong **array[3*3+1]

printf("%d  \n",*p[3*3+1]);  //overflow

printf("*(array[3]+1):%d  \n",*(array[3]+1));  //11

printf("%d \n ",*(p+4)[0]);   //overflow

/*all below is wrong. p1 is the pointer pointing array[0][0]

*  pi++ pointing array[0][1] and so on .the adder is sizeof(int)

*printf("%d  ",(*p1)[0]);//out:1

printf("%d  ",(*p1)[1]);//out:2

printf("%d  ",(*p1)[2]);//out:3

printf("(*p1)[3]:%d  ",(*p1)[3]);//out:4

printf("(*p1)[4]:%d  ",(*p1)[4]);//out:5

printf("(*p1)[5]:%d  ",(*p1)[5]);//out:6

printf("%d  ",*(p1+1)[0]);   //out:4

printf("%d  ",*(p1+2)[0]);   //out:7

printf("%d  ",*(p1+3)[0]);   //out:10

*/int i = 0;

for(i;i<12;i++)

printf("\n");

// for p2=array[0]

printf("%d  ",*p2);//1

printf("%d  ",*p2+10); //11 

printf("%d  ",*(p2+1));//2

printf("%d  ",*(p2+2));//3

printf("%d  ",*(p2+3));//4

printf("%d  ",*(p2+4));//5

printf("%d  ",*(p2+5));//6

printf("%d  ",*(p2+6));//7

printf("%d  ", *p2++); //1

printf("%d  ",*p2);

}

對於二維陣列用指標只能是上述p和p1和p2的用法,p3的賦值方法是錯誤的。

訪問二維陣列array[i][j]的方法有3種:*(array[i]+j)  array[i][j]  *(*(array+i)+j)

當int (*p)[3]=array時,p和array是一樣的,都是行指標,則在行指標進行加法運算時,是行間的移動,例如array+i 和p+i,是跨行的移動

int *p1=&array[0][0] 此時p1是一維指標,則現在將二維陣列看作乙個一維陣列,然後p1的移位操作來進行逐個元素進行訪問。

p2同p1

還要注意的是:int *a[3] 說明的先後順序是:p-->[3]-->*-->int  先說明陣列,再說明指標;

所以上面**中,如下:

printf("%d ",*(p+3)[1]); //overflow

printf("%d ",(*(p+3))[1]); //11

printf("%d ",(*(array+3))[1]);//11

printf("%d ",*(array+3)[1]);//overflow

printf("*array[3*3+1]:%d  \n",*array[3*3+1]); //overflow

這樣的訪問是錯誤的,因為array是行指標,所以array[3*3+1]已經越界了

C語言指標與二維陣列

二維陣列在概念上是二維的,有行和列,但在記憶體中所有的陣列元素都是連續排列的,它們之間沒有 縫隙 以下面的二維陣列 a 為例 int a 3 4 從概念上理解,a 的分布像乙個矩陣 0 1 2 3 4 5 6 7 8 9 10 11 但在記憶體中,a 的分布是一維線性的,整個陣列占用一塊連續的記憶體...

C語言指標與二維陣列

二維陣列在概念上是二維的,有行和列,但在記憶體中所有的陣列元素都是連續排列的,它們之間沒有 縫隙 以下面的二維陣列 a 為例 int a 3 4 從概念上理解,a 的分布像乙個矩陣 0 1 2 3 4 5 6 7 8 9 10 11 但在記憶體中,a 的分布是一維線性的,整個陣列占用一塊連續的記憶體...

C語言指標與二維陣列

二維陣列在概念上是二維的,有行和列,但在記憶體中所有的陣列元素都是連續排列的,它們之間沒有 縫隙 以下面的二維陣列 a 為例 int a 3 4 從概念上理解,a 的分布像乙個矩陣 0 1 2 3 4 5 6 7 8 9 10 11 但在記憶體中,a 的分布是一維線性的,整個陣列占用一塊連續的記憶體...