代码为test701.c
1 //This is c program code! 2 /* *=+=+=+=+* *** *=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= 3 * 文档信息: *** :~/WORKM/stutyCode/cCode/recipesProblemSolution/chapter07/test701.c 4 * 版权声明: *** :(魎魍魅魑)MIT 5 * 联络信箱: *** :guochaoxxl@163.com 6 * 创建时间: *** :2020年11月27日的上午08:44 7 * 文档用途: *** :数据结构与算法分析-c语言描述 8 * 作者信息: *** :guochaoxxl(http://cnblogs.com/guochaoxxl) 9 * 修订时间: *** :2020年第47周 11月27日 星期五 上午08:44 (第332天) 10 * 文件描述: *** :自行添加 11 * *+=+=+=+=* *** *+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+*/ 12 #include <stdio.h> 13 #include <stdlib.h> 14 15 int main(int argc, char **argv) 16 { 17 int size; 18 printf("please input the list size: "); 19 scanf("%d", &size); 20 int *list[size]; 21 for(int i = 0; i < size; i++){ 22 list[i] = (int *)calloc(size, sizeof(int)); 23 for(int j = 0; j < size; j++){ 24 *(list[i] + j) = i + j + 10; 25 } 26 } 27 28 printf("Displaying the values of items in list "); 29 for(int i = 0; i < size; i++){ 30 printf("List[%d]: ", i); 31 for(int j = 0; j < size; j++){ 32 printf("%d ", *(list[i] + j)); 33 } 34 printf(" "); 35 } 36 37 return 0; 38 }
代码说明:
1、第17~19行完成了列表大小的输入
2、第20行完成了指针列表的声明
3、第22行完成了内存的分配工作,对数组的内存分派,使用calloc函数更方便
4、第24行完成了指针列表中元素的赋值工作
5、第29~35行完成了列表元素的输出