1

I tried what is described in these links:

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

but I can't. Dimension of my array is 3 and I initialized it like this:

char* trips[N][2] = {{"ANKARA", "10:00"}, {"BURSA", "11:00"}, {"IZMIR", "12:00"}, {"ISTANBUL", "13:00"}, {"ANTALYA", "14:00"}} so how can I pass this matrix into function?

My code is here and it doesn't sort or effect my array.

#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);
  
}

Please dont mark my question as duplicate because I dont understand the solutions explained in the site and can't solve my problem.

Yunus YILDIRIM
  • 91
  • 1
  • 13
  • Stop merge sorting and start playing with a variable and a function until you can pass it correctly. – Millie Smith Oct 14 '17 at 22:40
  • 1
    I'm not answering since I'm not familiar with this area of C enough, but this is what I came up with: http://coliru.stacked-crooked.com/a/43b1f52fa50d17f1. I did this with the help of method 2 here: http://www.firmcodes.com/pass-2d-array-parameter-c-2/. 2d arrays are just one contiguous block in C: https://stackoverflow.com/questions/2565039/how-are-multi-dimensional-arrays-formatted-in-memory – Millie Smith Oct 14 '17 at 22:51
  • @MillieSmith tried it all day but I failed again and again. – Yunus YILDIRIM Oct 14 '17 at 22:56
  • I understand. C can be unforgiving. That code I gave above should get you moving again. – Millie Smith Oct 14 '17 at 22:58
  • Methinks first link solved passing problem but still mergeSort function does not effect my array. The compiler gives me this error: [Warning] assignment makes pointer from integer without a cast for this line of code :LA[i][S] = T[L+i][S]; – Yunus YILDIRIM Oct 14 '17 at 23:02
  • Yeah. Look at the type of the parameter and imagine what happens when you index into it twice. Then look at my code and see that I don't do that. Then look at the last link I linked above which states that memory is contiguous for 2d arrays. – Millie Smith Oct 14 '17 at 23:03
  • These short variable names are killing me, man. You're gonna have a hard time after you've slept too. – Millie Smith Oct 14 '17 at 23:04
  • 2
    `void merge(char**, int, int, int, SortType);` --> `void merge(char*[][2], int, int, int, SortType);` ... `mergeSort(trips, 0, N-1, SortByCity);` – BLUEPIXY Oct 14 '17 at 23:10
  • 1
    You've also run into another fun bug in C where `RA` is declared as a `char[][]` instead of a `char*[][]` because the star is only associated with the first declaration on that line. – Millie Smith Oct 14 '17 at 23:10
  • 1
    `char* LA[N1][2], RA[N2][2];` --> `char *LA[N1][2], *RA[N2][2];` – BLUEPIXY Oct 14 '17 at 23:10
  • thx bros. u re genius and funny boys but function still dont effect my array – Yunus YILDIRIM Oct 14 '17 at 23:14
  • Should I use strcpy() function to assign R arrays element to T? – Yunus YILDIRIM Oct 14 '17 at 23:17
  • `RA[j][S] = T[L+j][S];` : `L+j` is wrong. and `while (i < N1 && j < N2)` : It is not enough to process only in the case of `i < N1 && j < N2`. – BLUEPIXY Oct 14 '17 at 23:20
  • @JonahThunderbolt Look at here: [How to pass a multidimensional C array to a function?](https://stackoverflow.com/a/42447631/2913477) – MiniMax Oct 14 '17 at 23:27
  • 1
    `if(L < H)` --> `if(L >= H)`, `if (strcmp(LA[i][S], RA[j][S]))` --> `if (strcmp(LA[i][S], RA[j][S]) > 0)` – BLUEPIXY Oct 14 '17 at 23:43
  • @BLUEPIXY yeah epic fail L – Yunus YILDIRIM Oct 15 '17 at 00:22

0 Answers0