重要意義:間接賦值的意義,實現了模塊的功能划分,實現了軟件作品的分層,使得模塊接口和信息系統得以實現。
所謂二級指針就是指向指針的指針,其聲明形式如下
int *p=NULL int **p1=NULL; p1=&p;
一級指針*運算,從所指向內存空間取出數值(類比:一級指針是藏寶圖,所指向的內存空間是寶藏的存放的地點,寶藏是數值)
二級指針*運算,從所指向內存空間取出地址(類比:二級指針是一個寫有藏寶圖存放地點的紙條,所指向的內存空間是藏寶圖的存放的地點,藏寶圖是數值)
- 通過二級指針改變一級指針
#include <stdio.h> #include<stdlib.h> int main() { char *p1=NULL; char **p2=NULL; p1=0x11; printf("改變之前p1指向:%p\n",p1); p2=0x22; printf("改變之前p2指向:%p\n",p2); p2=&p1; *p2=100; printf("改變之后p2指向:%p\n",p2); printf("改變之后p1指向:%p\n",p1); printf("shit......%d\n",888); return 0; }
輸出結果:
- 間接指針賦值的意義
#include <stdio.h> #include<stdlib.h> #include<string.h> void modelfor2(char **line1,int *len1,char **line2,int *len2) { char *writeinln1 = (char*)malloc(100); *line1=writeinln1; strcpy(*line1,"九九那個艷陽天來嘿喲~"); *len1=strlen(writeinln1); printf("%s\n","==========================================="); char *writeinln2 = (char*)malloc(100); *line2=writeinln2; strcpy(*line2,"20多的弟弟呀,愛上那丁鍋鍋~"); *len2=strlen(writeinln2); } int main() { int len1=0; char *story1=NULL; int len2=0; char *story2=NULL; modelfor2(&story1,&len1,&story2,&len2); printf("第一行文本:%s\n",story1); printf("第一行文本長度:%d\n",len1); printf("第二行文本:%s\n",story2); printf("第二行文本長度:%d\n",len2); if (story1!=NULL) { free(story1); story1=NULL; } if (story2!=NULL) { free(story2); story2=NULL; } printf("%d\n",888); return 0; }
輸出結果:
間接賦值的意義,實現了模塊的功能划分,實現了軟件作品的分層,使得模塊接口和信息系統得以實現。