0

I tried to sort a struct array with merge sort, but somehow it'll only work when the struct variable is only one. This is the simplified code that i made:

#include <stdio.h>
#include <string.h>

struct name{
    int number;
};

void mergeSort(name arr[5], int left, int right){
    if(left == right){
        return;
    }
    
    int middle = (right - left) / 2 + left;
    mergeSort(arr, left, middle);
    mergeSort(arr, middle+1, right);
    
    int leftIdx = left,
        leftLimit = middle,
        rightIdx = middle+1,
        rightLimit = right,
        tempIdx = 0;
    name tempArr[right - left];
        
    while(leftIdx <= leftLimit && rightIdx <= rightLimit){
        if(arr[leftIdx].number < arr[rightIdx].number){
            tempArr[tempIdx] = arr[leftIdx];
            tempIdx++;
            leftIdx++;
        }else{
            tempArr[tempIdx] = arr[rightIdx];
            tempIdx++;
            rightIdx++;
        }
    }
    
    while(leftIdx <= leftLimit){
        tempArr[tempIdx] = arr[leftIdx];
        tempIdx++;
        leftIdx++;
    }
    while(rightIdx <= rightLimit){
        tempArr[tempIdx] = arr[rightIdx];
        tempIdx++;
        rightIdx++;
    }
    
    tempIdx = 0;
    for(int i=left; i<=right; i++){
        arr[i] = tempArr[tempIdx];
        tempIdx++;
    }
}

int main(){
    name arr[5];
    
    for(int i=0; i<5; i++){
        scanf("%d", &arr[i].number);
    }
    
    mergeSort(arr, 0, 4);
        
    for(int i=0; i<5; i++){
        printf("%d", arr[i].number);
    }
    
    return 0;
}

The code above works as intended. But as soon as i add a different type of variable to the struct, ran the code, and input the scanf, i ran into an error (not returning 0). for example:

struct name{
    int number;
    char name[101];
};

Anyone got any clue why is this happening and how if i want to put more variables inside the struct? I'm genuinely so confused when i found out the reason why my code wasn't working (and i have no clue how to fix this).

  • `name arr[5];` isn't a valid declaration. It should be `struct name arr[5];` - what C compiler are you using? – Ted Lyngmo Jan 17 '23 at 11:13
  • Calling a recursive merge sort function which expects a fixed array size doesn't make any sense. Use `name* arr`. Or well since you didn't typedef the struct, `struct name* arr`. – Lundin Jan 17 '23 at 11:13
  • what was the error you ran into? – user253751 Jan 17 '23 at 11:13
  • 1
    `while (leftIdx <= leftLimit) { tempArr[tempIdx] = arr[leftIdx];` is accessing the array out of bounds. Check the `tempIdx` index. – Ted Lyngmo Jan 17 '23 at 11:14
  • 2
    This would be a perfect time to learn how to [*debug*](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) your programs. For example by using a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to step through the code line by line while monitoring variables and their values. – Some programmer dude Jan 17 '23 at 11:15
  • The length of `tempArr` is too short. It should be `right - left + 1`. – Ian Abbott Jan 17 '23 at 11:26
  • In short: _"The code above works as intended"_ is not correct. The code above has _undefined behavior_ and may do just about anything. You may think it does the correct thing but it may also remove random files from your harddisk in the background. – Ted Lyngmo Jan 17 '23 at 13:24

0 Answers0