#define _CRT_SECURE_NO_WARNINGS #include"stdio.h" #include"stdlib.h" #include"string.h" char **sortArray1Array2_01(char **pArray1, int nArray1Num, char(*pArray2)[30], int nArray2Num, int *nArray3Num) { //第一維 int length = nArray1Num + nArray2Num;//總大度... int i = 0, j = 0; int k = 0; char **pTmp = (char **)malloc((length)*sizeof(char *)); //分配行數//就是分配多少個字符串。 // char* pTmp[Array1Num + nArray2Num] char ptmp[][]//感覺可以這麼說麼。分配2維的大小 for (i = 0; i < length; i++) { pTmp[i] = (char*)malloc(sizeof(char)* 30); //分配列數 //給列數分配大小; 每個字符串的長度; memset(pTmp[i], 0, sizeof(char)* 30); } for (i = 0; i < nArray1Num; i++) //把第一個字符串裡面的內容copy到 ptmp { strcpy(pTmp[i], pArray1[i]); } for (k = 0; k < nArray2Num; i++, k++) //往後面繼續疊加 { strcpy(pTmp[i], pArray2[k]); } for (i = 0; i < length; i++) //選擇法排序 { for (j = i + 1; j0) //交換; { char tmp[30] = { 0 }; strcpy(tmp, pTmp[i]); strcpy(pTmp[i], pTmp[j]); strcpy(pTmp[j], tmp); } } } *nArray3Num = length; //長度甩出去; return pTmp; //地址甩出去。 } void free_arr(char** arr, int length) //釋放。 { int i = 0; if (arr == NULL) { return; } for (i = 0; i < length; i++) { if (arr[i] != NULL) { free(arr[i]); } } if (arr != NULL) { free(arr); } } void main() { int i = 0; char **pArray3 = NULL; int nArray3Num = 0; //指針數組 char *array1[] = { "bbbbb", "aaaaa", "ccccc", "22222", "11111", "44444" }; //二維數組 char array2[10][30] = { "zzzz", "yyyy", "333333" }; pArray3 = sortArray1Array2_01(array1, 6, array2, 3, &nArray3Num); //甩出地址給pArray3. if (pArray3 == NULL) { printf("func sortArray1Array() err\n"); } for (i = 0; i < nArray3Num; i++) { printf("%s \n", pArray3[i]); } if (pArray3 != NULL) { free_arr(pArray3, nArray3Num); pArray3 = NULL; } system("pause"); }