char *number1[3] = {"asdf", "qwer", "zxcv"};
printf("%s", number1[0]);
這樣會印出asdf,不過我不理解為甚麼printf後面放的是number1[0]而非*number1[0]?
我的理解是number1[0]存放的應該是一個記憶體位置,而*number1[0]才是指向asdf的實際位置,不曉得對不對?
==================================
說明如下:
char *number1[3] = {"asdf", "qwer", "zxcv"};
number1是字元指標陣列.. 所以初指值指向3個字串位址
number1[0] --> "asdf"
number1[1] --> "qwer"
number1[2] --> "zxcv"
變數名稱 變數記憶體位址 內容(假設)
number1[0] &number1[0] 0x7ff0
number1[1] &number1[1] 0x7ff6
number1[2] &number1[2] 0x7fff
記憶體位址 內容
0 1 2 3 4
0x7ff0 'a' 's' 'd' 'f' '\0'
0x7ff6 'q' 'w' 'e' 'r' '\0'
0x7fff 'z' 'x' 'c' 'v' '\0'
(1) *number1[0] ==>'a'
(2) *(number1[0]+1) ==>'s'
(3) number[0] 的型態是 char * ,內容是0x7ff0
(4) number[0][0] 的型態是 char,內容是'a'
printf 的%s 輸出字串, 必須提供字串位址
printf("%s\n","asdf") ==> asdf
printf("%s\n",number1[0] ) ==> asdf
printf("%c\n",number1[0][0]) ==> a
printf("%c\n",number1[0][1]) ==> s
printf("%c\n",*number1[0] ) ==> a
printf("%c\n",*(number1[0]+1) ) ==> s
沒有留言 :
張貼留言