//已知下面几个数组 char str1[] = "arr1"; char str2[] = "aarr2"; char str3[] = "aaarr3"; char str4[] = "aaaarr4"; char str5[] = "aaaaarr5"; char str6[] = "aaaaaarr6"; char* a[][3] = {str1,str2,str3, str4,str5,str6}; char* b[][3] = {"aaaa1","bbbb","cccccc","ddddd","eee","fffff"}; char* c[][3] = {"cc","cc","cc"}; //现要求 //1.自己定义一个名为arr的数组,用来存储 a,b,c三个数组的地址当其元素成员。 //2.实现一个func()函数,此函数通过操作arr,来修改str1-str6的可修改字符串,均修改为"tt..t",并打印所有字符串(共15个小字符串)。
答案:
1 #include <stdio.h> 2 //2. 3 void func(char*(**p)[3],int arrLen,int aLen,int bLen,int cLen) 4 { 5 int i;//控制数组arr 6 int jRow,jCol;//控制数组a,b,c 7 int k;//控制str1-str6 8 //通过p修改str1-str2 为t..tt 9 i = 0; 10 for(jRow = 0;jRow < aLen;jRow++) 11 { 12 for(jCol = 0;jCol < 3;jCol++) 13 { 14 for(k = 0;*(*(*(*(p+i)+jRow)+jCol)+k);k++) 15 { 16 *(*(*(*(p+i)+jRow)+jCol)+k) = 't'; 17 } 18 } 19 } 20 //打印所有字符串 21 int len[3] = {aLen,bLen,cLen}; 22 23 for(i = 0;i < arrLen;i++) 24 { 25 for(jRow = 0;jRow<len[i];jRow++) 26 { 27 for(jCol = 0;jCol <3;jCol++)//*(*(p+i)+jRow)+jCol 28 { 29 printf("%s ", *(*(*(p+i)+jRow)+jCol)); 30 } 31 } 32 putchar(10); 33 } 34 } 35 36 37 int main(void) 38 { 39 40 char str1[] = "arr1"; 41 char str2[] = "aarr2"; 42 char str3[] = "aaarr3"; 43 char str4[] = "aaaarr4"; 44 char str5[] = "aaaaarr5"; 45 char str6[] = "aaaaaarr6"; 46 47 48 49 char* a[][3] = {str1,str2,str3, 50 str4,str5,str6}; 51 char* b[][3] = {"b1","bb2","bbb3","bbbb4","bbbbb5","bbbbbb6","bbbbbbb7","bbbbbbbb8","bbbbbbbbb9"}; 52 53 char* c[][3] = {"c","cc","ccc"}; 54 55 //1.自己定义一个数组arr,以及指向arr数组的指针。 56 57 //推导过程:char* a[][3] -> char* (*p)[3] = a; ->char* (*arr[])[3] 58 char* (*arr[])[3] = {a,b,c}; 59 60 //定义一个指向arr数组的指针的推导方式1:char* a[][3] 61 //->char*(*p)[3] = a; 62 //->char*(**p)[3] = &a; 63 //->char*(**p)[3] = arr; 64 //定义一个指向arr数组的指针的推导方式2: 65 //char* (*arr[])[3] -> 66 //直接将arr[]替换为*p ->char* (*(*p))[3]; 67 //char* (*(*p))[3] = arr;//用来定义func()的形参。 68 69 70 71 func(arr, sizeof(arr)/sizeof(*arr), sizeof(a)/sizeof(*a), sizeof(b)/sizeof(*b), sizeof(c)/sizeof(*c)); 72 73 return 0; 74 }