C语言 数组指针详解
1.一維數(shù)組指針
1.1 定義
數(shù)組指針,即指向數(shù)組的指針,對(duì)于一維數(shù)組char cTestOne[3] = {1, 2, 3},定義指向其的指針:char (*pOneArray)[3] = &cTestOne。
1.2 地址空間
&cTestOne和cTestOnep值相同,但意義不同,前者表示的是整個(gè)數(shù)組的首地址,后者表示的是數(shù)組第一個(gè)元素的地址。OneArray+1在地址空間上表示的是增加數(shù)組長度的地址,這與char (*pOneArray)[3]中的char類型和[3]長度相關(guān),我們定義char *pOneElement1 = cTestOne,pOneElement1+1在地址空間上表示的是增加數(shù)組單個(gè)元素長度的地址。
1.3 訪問
對(duì)于pOneArray,*pOneArray表示的首個(gè)元素的地址,**pOneArray則表示首個(gè)元素的值;對(duì)于pOneElement1 ,其本身表示首個(gè)元素的地址,*pOneElement1 表示首個(gè)元素的值。
1.4?測試
測試代碼:
char cTestOne[3] = {1, 2, 3};//指向一維數(shù)組char (*pOneArray)[3] = &cTestOne;//指向數(shù)組的第一個(gè)元素寫法1char *pOneElement1 = cTestOne;//指向數(shù)組的第一個(gè)元素寫法2char *pOneElement2 = &(cTestOne[0]);//地址空間printf("****************Address DisPlay****************\n");printf("&cTestOne = %p\n",&cTestOne);printf("cTestOne = %p\n",cTestOne);printf("&(cTestOne[0] = %p\n",&(cTestOne[0]));printf("pOneArray = %p\n",pOneArray);printf("pOneElement1 = %p\n",pOneElement1);printf("pOneElement2 = %p\n\n",pOneElement2);//步距測試printf("****************Step Length DisPlay****************\n");printf("&cTestOne + 1 = %p\n",&cTestOne + 1);printf("pOneArray + 1 = %p\n",pOneArray + 1);printf("pOneElement1 + 1 = %p\n",pOneElement1 + 1);printf("pOneElement2 + 1 = %p\n\n",pOneElement2 + 1);//元素訪問printf("****************Element Access DisPlay****************\n");printf("*pOneArray = %p\n",*pOneArray);printf("**pOneArray = %d\n",**pOneArray);printf("*(*pOneArray + 1) = %d\n",*(*pOneArray + 1));printf("*pOneElement1 = %d\n",*pOneElement1);printf("*(pOneElement1 + 1) = %d\n",*(pOneElement1 + 1));printf("*(pOneElement2 + 1) = %d\n\n",*(pOneElement2 + 1));測試結(jié)果:
2.二維數(shù)組指針
2.1 地址空間
對(duì)于二維數(shù)組,可以看做一個(gè)一維數(shù)組,其成員變量也是一維數(shù)組;定義char cTestTwo[2][3] = {1, 2, 3, 4, 5, 6},&cTestTwo表示的是整個(gè)數(shù)組的指針,&cTestTwo + 1步距為6(2乘以3,整個(gè)數(shù)組的長度);cTestTwo表示第0行的首地址,即cTestTwo[0]的地址,類似于上述章節(jié)中一維數(shù)組的&cTestOne,其步距為3(每行的長度);*cTestTwo表示的是數(shù)組第0個(gè)元素的地址,即cTestTwo[0][0]的地址。
2.2 訪問
由2.1章節(jié)描述,數(shù)組第0行地址為cTestTwo,第0個(gè)元素地址為*cTestTwo,訪問第0行第0個(gè)元素即為**cTestTwo;對(duì)于第0行第1個(gè)元素,即*(*cTestTwo + 1);對(duì)于第1行的第0個(gè)元素,其行起始地址為(cTestTwo + 1),訪問即為*(*(cTestTwo + 1)),對(duì)于第1行第1個(gè)元素,訪問即為*(*(cTestTwo + 1) +? 1);總結(jié):訪問第M行第N個(gè)元素,即*(*(cTestTwo + M) +? N)
2.3 測試
測試代碼:
//指向二維數(shù)組char cTestTwo[2][3] = {1, 2, 3, 4, 5, 6};char (*pTwoArray)[2][3] = &cTestTwo;char (*pTwoArrayOneLine)[3] = cTestTwo;//指向數(shù)組的第一個(gè)元素char *pTwoElement = (*cTestTwo);//地址空間printf("****************Address DisPlay****************\n");printf("&cTestTwo = %p\n",&cTestTwo);printf("cTestTwo = %p\n",cTestTwo);printf("*cTestTwo = %p\n",*cTestTwo);printf("pOneArray = %p\n",pTwoArray);printf("pTwoArrayOneLine = %p\n",pTwoArrayOneLine);printf("pTwoElement = %p\n\n",pTwoElement);//步距測試printf("****************Step Length DisPlay****************\n");printf("&cTestTwo + 1 = %p\n",&cTestTwo + 1);printf("cTestTwo + 1 = %p\n",cTestTwo + 1);printf("*cTestTwo + 1 = %p\n",*cTestTwo + 1);printf("pOneArray + 1 = %p\n",pTwoArray + 1);printf("pTwoArrayOneLine + 1 = %p\n",pTwoArrayOneLine + 1);printf("pTwoElement + 1 = %p\n\n",pTwoElement + 1);//元素訪問printf("****************Element Access DisPlay****************\n");printf("**cTestTwo = %d\n",**cTestTwo);printf("*(*cTestTwo + 1) = %d\n",*(*cTestTwo + 1));printf("*(*(cTestTwo + 1)) = %d\n",*(*(cTestTwo + 1)));printf("*(*(cTestTwo + 1) + 1) = %d\n\n",*(*(cTestTwo + 1) + 1));printf("**pTwoArrayOneLine = %d\n",**pTwoArrayOneLine);printf("*(*pTwoArrayOneLine + 1) = %d\n",*(*pTwoArrayOneLine + 1));printf("*(*(pTwoArrayOneLine + 1)) = %d\n",*(*(pTwoArrayOneLine + 1)));printf("*(*(pTwoArrayOneLine + 1) + 1) = %d\n\n",*(*(pTwoArrayOneLine + 1) + 1));printf("*pTwoElement = %d\n",*(pTwoElement + 0));printf("*(pTwoElement + 1) = %d\n",*(pTwoElement + 1));printf("*(pTwoElement + 3) = %d\n",*(pTwoElement + 3));printf("*(pTwoElement + 4) = %d\n\n",*(pTwoElement + 4));測試結(jié)果:
總結(jié)
以上是生活随笔為你收集整理的C语言 数组指针详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。