Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main(){
- /************** pointer array *****************/
- const int suit_size = 4;
- // 觀念測試1:
- char* p = "diamond"; // 初始化 - 會自動幫忙配置memory
- printf("%s\n",p);
- // 觀念測試2:
- char suit1[suit_size] = {'d','t','a','s'};
- // 觀念測試2(錯誤示範):
- // error: too many initializers for 'char[4]'
- // char suit2[suit_size] = {"diamond","tiger","apple","skytowel"};
- // 觀念測試3:
- const int suit_col_size = 10; // 必須先給長度,且大於下列最大的字串長度
- char suit3[suit_size][suit_col_size] = {"diamond","tiger","apple","skytowel"};
- // 重點:
- // 因為是指標(幾乎可看成是array來操作,所以能夠用"AABBCC"的初始化方式動態配置memory)
- char* suit4[suit_size] = {"diamond","tiger","apple","skytowel"};
- for (int i = 0; i < suit_size; i++){
- printf("%c ", *suit4[i]); // 拿此指標(array)的第一個元素
- }
- printf("\n");
- for (int i = 0; i < suit_size; i++){
- printf("%s ", suit4[i]); // 拿此指標(array)的字串(直到撞到'\0')
- }
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement