这两天在学习扫地僧的课程(二级指针三种内存模型)时,最后的作业是把“第一种内存模型和第二种内存模型的内容复制到第三种内存模型中,并进行排序”。老师预定义了前两种内存模型的内容,和对应的函数实现接口,如下:
char *p1[] = { "aaaaa", "cccccc", "bbbbb" };
char buf2[10][30] = { "111111", "3333333", "2222222" };
函数接口:
int sort(char ** myp1/*in*/, int num1/*in*/, char(*myp2)[30]/*in*/, int num2/*in*/, char ***myp3/*out*/, int *pNum3/*out*/);
既然老师希望在sort()函数解决问题,我的思路是:
- 先获得传入的两个字符串数组中,最长的字符串长度;
- 取两个输入的字符串数组的成员数量之和,并且获得最长的那个字符串的长度之后,根据这两个参数再堆上申请空间;
- 分别复制两个字符串数组到新的堆空间上;
- 针对堆空间的字符串数组进行排序,最后输出排好序的字符串。
- 释放申请的堆空间。
思路还比较清晰,然后就自己实现代码了。首先我自己定义的函数接口为:
// 获取最长的字符串长度
int getRowLen(char **myp1/*in*/, int num1/*int*/, int *pLen)
// 申请堆空间
int getArray3(char ***p, int RowNum, int RowLen)
// 复制字符串数组
int copyArray(char **myp1/*in*/, int num1/*in*/, char **myp3/*in*/, int *pMyp3Row/*in, out*/)
// 释放堆空间
int getArray3_Free(char ***p3, int p3num)
// 最后是题目给定的函数接口,主要功能在该函数中实现
int sort(char ** myp1/*in*/, int num1/*in*/, char(*myp2)[30]/*in*/, int num2/*in*/, char ***myp3/*out*/, int *pNum3/*out*/)
测试案例如下:
int main()
{
int nRet = 0;
char *p1[] = { "aaaaa", "cccccc", "bbbbb" };
char buf2[10][30] = { "111111", "3333333", "2222222" };
char **p3 = NULL;
int len1, len2, len3, i = 0;
len1 = sizeof(p1) / sizeof(*p1);
len2 = 3;
nRet = sort(p1, len1, buf2, len2, &p3, &len3);
if (nRet != 0) {
printf("func sort() err: %d \n", nRet);
return nRet;
}
printf("after sorting...\n");
for (; i < len3; i++) {
printf("%s \n", p3[i]);
}
getArray3_Free(&p3, len3);
system("pause");
}
在编译的时候就遇到了困惑,编译器报错:
Error C2664 'int copyArray(char **,int,char **,int )': cannot convert argument 1 from 'char ()[30]' to 'char **'
我在追溯问题的时候一直以为是指针传输的问题,但是想不到更具体的可疑点。最后在做实验的时候发现,有两个函数接口有同样的问题,getRowLen() 和 copyArray()都是把试图原数组的地址传进来,可是,每次传入的数组是buf2[10]
[30]的时候就会报错,原因在于:
设计的函数参数列表中没有指定确定的维度信息,在传输buf2[10][30]的时候,因为sort()指定了维度信息char(myp2)[30],所以编译器知道myp2的第二维是30个char类型的长度,至于myp2中有多少个字符串,在传入myp2的时候编译器必然是已经计算出myp2中包含的字符串的个数了。但是,如果我们不指定清楚第二维的大小,那么编译器在分配空间时走到这里就会遇到一个“摩棱两个”的问题:第二维是多大不知道,那么不论分配多少空间都是有可能“踩地址”的!*所以编译器不得不在这里报错了。
知道了原因后,我不得不重新设计了函数接口如下,明显是比较笨拙的:
// 获取最长的字符串长度
int getRowLen(char **myp1/*in*/, int num1/*int*/, int *pLen)
// 获取最长的字符串长度:用于buf2
int getRowLen_1(char (*myp1)[30]/*in*/, int num1/*int*/, int *pLen)
// 申请堆空间
int getArray3(char ***p, int RowNum, int RowLen)
// 复制字符串数组
int copyArray(char **myp1/*in*/, int num1/*in*/, char **myp3/*in*/, int *pMyp3Row/*in, out*/)
// 复制字符串数组:用于buf2
int copyArray_1(char (*myp1)[30]/*in*/, int num1/*in*/, char **myp3/*in*/, int *pMyp3Row/*in, out*/)
// 释放堆空间
int getArray3_Free(char ***p3, int p3num)
// 最后是题目给定的函数接口,主要功能在该函数中实现
int sort(char ** myp1/*in*/, int num1/*in*/, char(*myp2)[30]/*in*/, int num2/*in*/, char ***myp3/*out*/, int *pNum3/*out*/)
这样一来,就相当于针对二级指针的第一种内存模型和第二种内存模型分别设计了函数接口,虽然功能实现了,可是毕竟有“重复造轮子”之嫌。
不过,通过绕一个弯我也深刻体会到了指针在C语言中带来的便利之处。假如在测试案例中,都这样定义字符串数组:
buf0[3][30] = { "aaaaa", "cccccc", "bbbbb" };
buf1[3][20] = { "111111", "3333333", "2222222" };
buf2[3][10] = { "hello", "world", "C Program Language" };
这样在设计函数接口的时候,就必须为所有的二维长度设计好函数,这样明显很笨拙。但是如果是使用指针来定义字符串数组时,不同的字符串数组就可以使用一套函数接口来工作了。这样才符合代码简洁之道!
YES POINTER!
以下是所有源代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int getArray3_Free(char ***p3, int p3num)
{
int nRet = 0;
int i = 0;
char **pTmp = NULL;
if (p3 == NULL) {
nRet = -1;
printf("func getArray3_Free:: detects that (p3 == NULL), error code: %d \n", nRet);
goto END;
}
pTmp = *p3;
if (pTmp == NULL) {
nRet = -2;
printf("func getArray3_Free:: detects that (pTmp == NULL), error code: %d \n", nRet);
goto END;
}
for (; i < p3num; i++) {
if (pTmp[i] != NULL) {
free(pTmp[i]);
pTmp[i] = NULL;
}
}
free(pTmp);
*p3 = NULL;
goto END;
END:
return nRet;
}
int getArray3(char ***p, int RowNum, int RowLen)
{
int nRet = 0;
int i = 0;
char **pTmp = NULL;
//int nLen = 60;
if (p == NULL) {
nRet = -1;
printf("func getArray3:: detects that (p == NULL), error code: %d \n", nRet);
goto END;
}
pTmp = (char **)malloc(sizeof(char *) * RowNum);
if (pTmp == NULL) {
nRet = -2;
printf("func getArray3:: detects that (pTmp == NULL), error code: %d \n", nRet);
goto END;
}
for (; i < RowNum; i++) {
pTmp[i] = (char *)malloc(sizeof(char) * RowLen);
}
*p = pTmp;
goto END;
END:
return nRet;
}
int getRowLen(char **myp1/*in*/, int num1/*in*/, int *pLen)
{
int nRet = 0;
int nLen = 0, nLenRow, i = 0;
if (myp1 == NULL) {
nRet = -1;
printf("func getRowLen:: detectst that (myp1 == NULL), error code: %d \n", nRet);
goto END;
}
for (; i < num1; i++) {
nLenRow = strlen(myp1[i]) + 1;
nLen = (nLenRow > nLen) ? nLenRow : nLen;
}
*pLen = nLen;
goto END;
END:
return nRet;
}
int getRowLen_1(char (*myp1)[30]/*in*/, int num1/*int*/, int *pLen)
{
int nRet = 0;
int nLen = 0, nLenRow, i = 0;
if (myp1 == NULL) {
nRet = -1;
printf("func getRowLen:: detectst that (myp1 == NULL), error code: %d \n", nRet);
goto END;
}
for (; i < num1; i++) {
nLenRow = strlen(myp1[i]) + 1;
nLen = (nLenRow > nLen) ? nLenRow : nLen;
}
*pLen = nLen;
goto END;
END:
return nRet;
}
int copyArray(char **myp1/*in*/, int num1/*in*/, char **myp3/*in*/, int *pMyp3Row/*in, out*/)
{
int nRet = 0;
int i = 0, myp3Row;
char **pTmp = NULL;
if (myp1 == NULL || myp3 == NULL || myp3 == NULL || pMyp3Row == NULL) {
nRet = -1;
printf("func sort:: detects that (myp1== NULL || myp3 == NULL || pNum3 == NULL || pMyp3Row == NULL), error code: %d \n", nRet);
goto END;
}
pTmp = myp3;
if (pTmp == NULL) {
nRet = -2;
printf("func sort:: pTmp == NULL), error code: %d \n", nRet);
goto END;
}
myp3Row = *pMyp3Row;
for (; i < num1; i++) {
strncpy(pTmp[myp3Row], myp1[i], strlen(myp1[i]));
pTmp[myp3Row][strlen(myp1[i])] = '\0';
printf("pTmp[%d]: %s \n", myp3Row, pTmp[myp3Row]);
myp3Row += 1;
}
*pMyp3Row = myp3Row;
END:
return nRet;
}
int copyArray_1(char (*myp1)[30]/*in*/, int num1/*in*/, char **myp3/*in*/, int *pMyp3Row/*in, out*/)
{
int nRet = 0;
int i = 0, myp3Row;
char **pTmp = NULL;
if (myp1 == NULL || myp3 == NULL || myp3 == NULL || pMyp3Row == NULL) {
nRet = -1;
printf("func sort:: detects that (myp1== NULL || myp3 == NULL || pNum3 == NULL || pMyp3Row == NULL), error code: %d \n", nRet);
goto END;
}
pTmp = myp3;
if (pTmp == NULL) {
nRet = -2;
printf("func sort:: pTmp == NULL), error code: %d \n", nRet);
goto END;
}
myp3Row = *pMyp3Row;
for (; i < num1; i++) {
strncpy(pTmp[myp3Row], myp1[i], strlen(myp1[i]));
pTmp[myp3Row][strlen(myp1[i])] = '\0';
myp3Row += 1;
}
*pMyp3Row = myp3Row;
END:
return nRet;
}
int sort(char ** myp1/*in*/, int num1/*in*/, char(*myp2)[30]/*in*/, int num2/*in*/, char ***myp3/*out*/, int *pNum3/*out*/)
{
int nRet = 0;
int nLenRowMax1 = 0, nLenRowMax2 = 0, nLenRow = 0;
int nRowIdx = 0;
int i, j;
char ** pTmp = NULL;
char *pTmpCg = NULL;
if (myp1 == NULL || myp2 == NULL || pNum3 == NULL) {
nRet = -1;
printf("func sort:: detects that (myp1== NULL || myp2 == NULL || pNum3 == NULL), error code: %d \n", nRet);
goto END;
}
*pNum3 = num1 + num2; // to get the size of string group 3
#if (0)
*myp3 = (char**)malloc(sizeof(char *) * sizeof(*pNum3)); // to ask address for myp3
if (myp3 == NULL) {
nRet = -2;
printf("func sort:: detects that (myp3 == NULL), error code: %d \n", nRet);
goto END;
}
#endif
nRet = getRowLen(myp1, num1, &nLenRowMax1);
if (nRet != 0) {
nRet = -3;
printf("func sort:: detects that (nRet != 0), error code: %d \n", nRet);
goto END;
}
printf("nLenRowMax1: %d \n", nLenRowMax1);
nRet = getRowLen_1(myp2, num2, &nLenRowMax2);
if (nRet != 0) {
nRet = -4;
printf("func sort:: detects that (nRet != 0), error code: %d \n", nRet);
goto END;
}
nLenRow = (nLenRowMax1 > nLenRowMax2) ? nLenRowMax1 : nLenRowMax2; // to get the row size of new array
nRet = getArray3(myp3, *pNum3, nLenRow); // to put space for myp3
if (nRet != 0) {
nRet = -4;
printf("func sort:: detects that (nRet != 0), error code: %d \n", nRet);
goto END;
}
nRet = copyArray(myp1, num1, *myp3, &nRowIdx);
if (nRet != 0) {
nRet = -5;
printf("func sort:: detects that (nRet != 0), error code: %d \n", nRet);
goto END;
}
nRet = copyArray_1(myp2, num2, *myp3, &nRowIdx);
//nRet = copyArray(myp2, num2, *myp3, &nRowIdx);
if (nRet != 0) {
nRet = -5;
printf("func sort:: detects that (nRet != 0), error code: %d \n", nRet);
goto END;
}
printf("before sorting: \n");
for (i=0; i < *pNum3; i++) {
printf("%s \n", (*myp3)[i]);
}
if (nRowIdx != *pNum3) {
*pNum3 = nRowIdx - 1;
printf("111111111111111111");
}
//*******sort********
pTmp = *myp3;
for (i = 0; i < *pNum3; i++) {
for (j = i + 1; j < *pNum3; j++) {
if (strcmp(pTmp[i], pTmp[j]) > 0) {
pTmpCg = pTmp[i];
pTmp[i] = pTmp[j];
pTmp[j] = pTmpCg;
}
}
}
goto END;
END:
return nRet;
}
int main()
{
int nRet = 0;
char *p1[] = { "aaaaa", "cccccc", "bbbbb" };
char buf2[10][30] = { "111111", "3333333", "2222222" };
char **p3 = NULL;
int len1, len2, len3, i = 0;
len1 = sizeof(p1) / sizeof(*p1);
len2 = 3;
nRet = sort(p1, len1, buf2, len2, &p3, &len3);
if (nRet != 0) {
printf("func sort() err: %d \n", nRet);
return nRet;
}
printf("after sorting...\n");
for (; i < len3; i++) {
printf("%s \n", p3[i]);
}
getArray3_Free(&p3, len3);
printf("hello world....\n");
system("pause");
}