日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

C语言 数组指针详解

發布時間:2023/12/14 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C语言 数组指针详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.一維數組指針

1.1 定義

數組指針,即指向數組的指針,對于一維數組char cTestOne[3] = {1, 2, 3},定義指向其的指針:char (*pOneArray)[3] = &cTestOne。

1.2 地址空間

&cTestOne和cTestOnep值相同,但意義不同,前者表示的是整個數組的首地址,后者表示的是數組第一個元素的地址。OneArray+1在地址空間上表示的是增加數組長度的地址,這與char (*pOneArray)[3]中的char類型和[3]長度相關,我們定義char *pOneElement1 = cTestOne,pOneElement1+1在地址空間上表示的是增加數組單個元素長度的地址。

1.3 訪問

對于pOneArray,*pOneArray表示的首個元素的地址,**pOneArray則表示首個元素的值;對于pOneElement1 ,其本身表示首個元素的地址,*pOneElement1 表示首個元素的值。

1.4?測試

測試代碼:

char cTestOne[3] = {1, 2, 3};//指向一維數組char (*pOneArray)[3] = &cTestOne;//指向數組的第一個元素寫法1char *pOneElement1 = cTestOne;//指向數組的第一個元素寫法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));

測試結果:

2.二維數組指針

2.1 地址空間

對于二維數組,可以看做一個一維數組,其成員變量也是一維數組;定義char cTestTwo[2][3] = {1, 2, 3, 4, 5, 6},&cTestTwo表示的是整個數組的指針,&cTestTwo + 1步距為6(2乘以3,整個數組的長度);cTestTwo表示第0行的首地址,即cTestTwo[0]的地址,類似于上述章節中一維數組的&cTestOne,其步距為3(每行的長度);*cTestTwo表示的是數組第0個元素的地址,即cTestTwo[0][0]的地址。

2.2 訪問

由2.1章節描述,數組第0行地址為cTestTwo,第0個元素地址為*cTestTwo,訪問第0行第0個元素即為**cTestTwo;對于第0行第1個元素,即*(*cTestTwo + 1);對于第1行的第0個元素,其行起始地址為(cTestTwo + 1),訪問即為*(*(cTestTwo + 1)),對于第1行第1個元素,訪問即為*(*(cTestTwo + 1) +? 1);總結:訪問第M行第N個元素,即*(*(cTestTwo + M) +? N)

2.3 測試

測試代碼:

//指向二維數組char cTestTwo[2][3] = {1, 2, 3, 4, 5, 6};char (*pTwoArray)[2][3] = &cTestTwo;char (*pTwoArrayOneLine)[3] = cTestTwo;//指向數組的第一個元素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));

測試結果:

總結

以上是生活随笔為你收集整理的C语言 数组指针详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。