传智播客_C提高讲义 下载本文

欢迎阅读 for (j=0; j<5; j++) { a[i][j] = tmp ++; } } printfArray411((int *)a, 15); system(\} 4.2.5多维数组做函数参数技术推演 1、C语言中只会以机械式的值拷贝的方式传递参数(实参把值传给形参) int fun(char a[20], size_t b) { ? ?printf(\} 原因1:高效 原因2: C语言处理a[n]的时候,它没有办法知道n是几,它只知道&n[0]是多少,它的值作为参数传递进去了 虽然c语言可以做到直接int fun(char a[20]),然后函数能得到20这个数字,但是,C没有这么做。 2、二维数组参数同样存在退化的问题 二维数组可以看做是一维数组 二维数组中的每个元素是一维数组 二维数组参数中第一维的参数可以省略 void f(int a[5]) ====》void f(int a[]); ===》 void f(int* a); void g(int a[3][3])====》 void g(int a[][3]); ====》 void g(int (*a)[3]); 3、等价关系 数组参数 等效的指针参数 指针 char* 指针的指针 char **a 一维数组 char a[30] 指针数组 char *a[30] 二维数组 char a[10][30] 数组的指针 char(*a)[30] 4.3指针数组的应用场景 指针数组的两种用法(菜单 命令行) 操作系统拉起应用 在框架下干活 字符数组自我结束标志 // NULL 0 '\\0' 4.4强化训练

课堂考试“上黑板”

int sort(char *p[], int count, char **p, int *ncount); int sort(char *p[], int count, char (*p)[30], int *ncount); 欢迎阅读

欢迎阅读

int sort(char (*p)[30], int ncount, char **p, int *ncount);

//把第一种内存模型第二种内存模型结果copy到第三种内存模型中,并排序,打印 char ** sort(char **p1, int num1, char (*p)[30], int num2, int *num3 ); // #include \#include \#include \int getArray3_Free(char **p3, int p3num) { int i; if (p3 == NULL) { return -1; } for (i=0; i0) { temp=tmpp3[i]; tmpp3[i]=tmpp3[j]; tmpp3[j]=temp; } } } //通过间接赋值,把结果甩给实参 *num3=tmpNum3; *myp3 = tmpp3; //*0 = 100; return ret; } char **getArray3(char **myp1, int num1, char (*myp2)[30], int num2, int *num3) { int i,j; int tmpNum3 = 0; char **tmpp3 = NULL; char *temp; if (myp1==NULL || myp2==NULL ||num3==NULL ) { return NULL; } //准备内存 tmpNum3 = num1 + num2; //分配第一维 tmpp3 = (char **)malloc(tmpNum3 * sizeof(char *)); if (tmpp3 == NULL) { return NULL; } //分配第二维 把第一种内存模型数据和第二种内存模型数据,copy到第3中内存模型中 for (i=0; i0) { temp=tmpp3[i]; tmpp3[i]=tmpp3[j]; tmpp3[j]=temp; } } } *num3=tmpNum3; return tmpp3; } void main() { int num3 = 0, i = 0; int ret = 0; char p2[4][30] = {\ char **p3 = NULL; char ***myerrp3 = NULL; //p3 = getArray3(p1, 3, p2, 4, &num3); //ret = getArray3_2(p1,3, p2, 4, &p3, &num3); ret = getArray3_2(p1,3, p2, 4, 0, &num3); //错误做法 if (ret != 0) { return ; } for (i=0; i