指針與指針內存儲地址的關系


 1 #pragma region 指針++,指針內存儲的地址增加m位,m根據存儲內容的所占字節數的大小決定
 2 
 3 //這里通過一個順序表來說明這一關系
 4 using namespace std;
 5 typedef int Status;
 6 typedef int ElemType;
 7 constexpr auto MAXSIZE = 100;// 最大長度;
 8 typedef struct SqList {
 9     ElemType *elem;//基地址
10     int length;//實際長度
11 }SqList;
12 
13 Status InitLsit_Sq(SqList &L) {//構造一個空的順序表;&表示可以雙向傳遞,實參把值傳給形參,形實合一
14     L.elem = new ElemType[MAXSIZE];//為順序表分配空間;new申請MAXSIZE個ElemType類型的空間
15     if (!L.elem) exit(OVERFLOW);//存儲空間分配失敗
16     L.length = 0;//空表長度為0
17     return 1;
18 }
19 
20 Status ListInsert_Sq(SqList &L, int i, ElemType e) {//有返回值
21     if (i<1 || i>L.length + 1) return 0;//判斷插入位置是否合法
22     if (L.length == MAXSIZE) return 0;//判斷存儲空間是否已滿
23     for (int j = L.length; j >= i - 1; j--) {//n到i元素向后移動
24         L.elem[j + 1] = L.elem[j];
25     }
26     L.elem[i - 1] = e;//e放置第i個位置
27     ++L.length;//表長+1
28     return 0;
29 }
30 
31 //根據元素內容e獲取元素位置i
32 int LocateElem(SqList L, ElemType e) {
33     //查找不影響線性表長度和內容,實參內容值已被用戶給出
34     for (int i = 0; i < L.length; i++)
35         if (L.elem[i] == e) return i + 1;
36     return 0;//當for語句表達式為假||if表達式為假時執行return 0
37 }
38 #pragma endregion
39 
40 int main()
41 {
42     SqList B;//B為創建順序表的名稱
43     InitLsit_Sq(B);
44     //順序表B的值依次為2、3、4
45     ListInsert_Sq(B, 1, 2);
46     ListInsert_Sq(B, 2, 3);
47     ListInsert_Sq(B, 3, 4);
48     int *p1 = &(B.elem[0]);
49     int *p2 = &(B.elem[1]);
50     int *p3 = &(B.elem[2]);
51     int *p4 = p1++;//先賦值給p4,然后作p1++
52     printf("%0x, %0x, %d, %d\n", p1, &(B.elem[0]), *p1, (B.elem[0]));
53     printf("%0x, %0x, %d, %d\n", p2, &(B.elem[1]), *p2, (B.elem[1]));
54     printf("%0x, %0x, %d, %d\n", p3, &(B.elem[2]), *p3, (B.elem[2]));
55     printf("%0x, %0x, %d, %d\n", p4, &(B.elem[0]), *p4, (B.elem[0]));
56 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM