我尝试了这些链接中描述的内容:
How can I pass character pointer reference to function and get affected value back?
Passing pointers (matrix) to a function in c
Passing an array as an argument to a function in C
但我不能。我的数组的维度为3,我将其初始化为:
char * trips [N] [2] = {{“ANKARA”,“10:00”},{“BURSA”,“11:00”},{“IZMIR”,“12:00”}, {“ISTANBUL”,“13:00”},{“ANTALYA”,“14:00”}} 那我怎么能把这个矩阵传递给函数呢?
我的代码在这里,它不会对我的数组进行排序或影响。
#include <stdio.h>
#include <stdlib.h>
#define N 10
typedef enum {
SortByCity,
SortByHour
}SortType;
void merge(char**, int, int, int, SortType);
void mergeSort(char**, int, int, SortType);
void mergeSortByHour(char**, int, int, SortType);
int main(int argc, char *argv[]) {
int i;
char* trips[N][2] = {{"ANKARA", "10:00"}, {"BURSA", "11:00"}, {"IZMIR", "12:00"}, {"ISTANBUL", "13:00"}, {"ANTALYA", "14:00"}, {"ANKARA", "11:00"}, {"ISTANBUL", "13:00"}, {"ANKARA", "11:30"}, {"BURSA", "21:00"} , {"BURSA", "13:00"}};
mergeSort(**trips, 0, N-1, SortByCity);
for(i = 0; i < N; i++){
printf("%-10s %s\n", trips[i][0], trips[i][1]);
}
return 0;
}
void merge(char** T, int L, int M, int H, SortType S){
int i, j, k;
int N1 = M - L + 1;
int N2 = H - M;
char* LA[N1][2], RA[N2][2];
for (i = 0; i < N1; i++){
LA[i][S] = T[L+i][S];
LA[i][1-S] = T[L+i][1-S];
}
for (j = 0; j < N2; j++){
RA[j][S] = T[L+j][S];
RA[j][1-S] = T[L+j][1-S];
}
i = 0;
j = 0;
k = L;
while (i < N1 && j < N2)
{
if (strcmp(LA[i][S], RA[j][S]))
{
T[k][S] = RA[j][S];
T[k][1-S] = RA[j][1-S];
j++;
}
else
{
T[k][S] = LA[i][S];
T[k][1-S] = LA[i][1-S];
i++;
}
k++;
}
}
void mergeSort(char** T, int L, int H, SortType S){
if(L < H)
return;
int M = (L + H) / 2;
mergeSort(T, L, M, S);
mergeSort(T, M+1, H, S);
merge(T, L, M, H, S);
}
请不要将我的问题标记为重复,因为我不理解网站中解释的解决方案,但无法解决我的问题。