1 #if 1 2 #define _CRT_SECURE_NO_WARNINGS 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <string.h> 6 #include <limits.h> 7 #include <ctype.h> 8 #include <math.h> 9 //输入特性:主调函数分配内存,被调函数使用 10 void printArray(int** array, int len) { 11 for (int i = 0; i < len; i++) { 12 printf("%d\n",*array[i]); 13 } 14 } 15 test01() { 16 //在堆区分配内存 17 int* array = malloc(sizeof(int)* 5); 18 //在栈上创建5个元素 19 int a1 = 10; 20 int a2 = 20; 21 int a3 = 30; 22 int a4 = 40; 23 int a5 = 50; 24 25 array[0] = &a1; 26 array[1] = &a2; 27 array[2] = &a3; 28 array[3] = &a4; 29 array[4] = &a5; 30 //打印数组 31 printArray(array, 5); 32 33 if (array != NULL) { 34 free(array); 35 array = NULL; 36 37 } 38 } 39 //在栈上分配内存 40 void test02() { 41 int* array[5]; 42 for (int i = 0; i < 5; i++) { 43 array[i] = malloc(sizeof(int)); 44 *(array[i]) = 100 + i; 45 } 46 //打印数组 47 int len = sizeof(array) / sizeof(int*);//这儿要注意的是计算的是数组元素还是坐标 48 printArray(array, len); 49 50 for (int i = 0; i < len; i++) { 51 if (array[i] != NULL) { 52 free(array[i]); 53 array[i] = NULL; 54 } 55 } 56 } 57 int main(int argc, char* argv[]) 58 { 59 60 test01(); 61 system("pause"); 62 63 return 0; 64 } 65 #endif
19:57
原文:https://www.cnblogs.com/MyLoveLiJuan/p/11852374.html