程序代碼
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
/*
二維數組表示方法如下
1.數組名
2.指針數組
3.數組指針(只能用來存儲二維數組的數組名[即二維數組的首地址],不可用來存儲內容)
*/
// 二維數組
// ① char testStr1[10][6] = {"begin", "ifgbd", "while", "dosfa", "abend"}; // 字符串的存儲方式 1.字符數組 2.字符指針變量
char testStr1[10][5] = {{'b','e','g','i','n'}, {'i','f','g','b','d'}, {'w','h','i','l','e'}, {'d','o','s','f','a'}, {'a','b','e','n','d'}};
/*
注意:
char str1[10] = {'s','t','u','d','e','n','t'};
在內存中的存放形式為:
s t u d e n t 【6】
char str2[10] = "student"; // 在存儲字符串時末尾自動加上字符串的結束標志'\0'
在內存中的存放形式為:
s t u d e n t \0 【7】這是為什么前面 testStr1[10][6] 要這樣定義,因為每個字符串的總長度為5,但是"begin"以這樣的
形式就會在內存中自動多存儲一個結束標志'\0',所以要多定義一個空間
"student" 無論是單獨成立的,還是放在大括號里,其存儲在內存中都是會自動加上字符串的結束標志'\0'
*/
// 指針數組
char *testStrs[5] = {"begin", "ifgbd", "while", "dosfa", "abend"};
// 數組指針
// 與前面的① 相對應char (*testStr)[6];
char (*testStr)[5]; // 注意這里不能像前面兩種形式那樣賦值
int main(){
int i = 0;
int j = 0;
for(i = 0; i < 5; i++){
for(j = 0; j < 5; j++){
printf("%c",*(*(testStrs + i) + j)); // printf("%c",*(testStrs[i] + j)); testStrs[i] <==> *(testStrs + i)
}
printf("\n");
}
printf("\n");
testStr = testStr1;
for(i = 0; i < 5; i++){
for(j = 0; j < 5; j++){
printf("%c",*(*(testStr + i) + j));
}
printf("\n");
}
return 0;
}
運行結果
