Advertisement
uopspop

Untitled

Dec 29th, 2016
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main(){
  5.  
  6. /************** pointer array *****************/
  7.     const int suit_size = 4;
  8. // 觀念測試1:
  9.     char* p = "diamond"; // 初始化 - 會自動幫忙配置memory
  10.     printf("%s\n",p);
  11.  
  12. // 觀念測試2:
  13.     char suit1[suit_size] = {'d','t','a','s'};
  14.  
  15. // 觀念測試2(錯誤示範):
  16. // error: too many initializers for 'char[4]'
  17. //    char suit2[suit_size] = {"diamond","tiger","apple","skytowel"};
  18.  
  19. // 觀念測試3:
  20.     const int suit_col_size = 10; // 必須先給長度,且大於下列最大的字串長度
  21.     char suit3[suit_size][suit_col_size] = {"diamond","tiger","apple","skytowel"};
  22.  
  23. // 重點:
  24.     // 因為是指標(幾乎可看成是array來操作,所以能夠用"AABBCC"的初始化方式動態配置memory)
  25.     char* suit4[suit_size] = {"diamond","tiger","apple","skytowel"};
  26.  
  27.     for (int i = 0; i < suit_size; i++){
  28.         printf("%c ", *suit4[i]); // 拿此指標(array)的第一個元素
  29.     }
  30.     printf("\n");
  31.  
  32.     for (int i = 0; i < suit_size; i++){
  33.         printf("%s ", suit4[i]); // 拿此指標(array)的字串(直到撞到'\0')
  34.     }
  35.     printf("\n");
  36.  
  37.     return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement