1

I was trying to create a function that takes a char** as a parameter and returns a char** type. The purpose of this function is to sort a list of strings alphabetically. I have this code and I can't figure out why my code doesn't work. I saw this answer "Returning an array of strings in C", but I still can't figure out why the following code crushes...

Here is my code:

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

char** a_to_z_ordering (char**, int);

int main(int argc, char **argv) {
    char** unordered;
    //char** ordered;

    unordered = malloc(50*150); //is it ok to use more space? (In case my max is what the numbers declare)
    //ordered = malloc(50*150); //should I declare like this?

    for(int i = 0; i<50; i++) {
        unordered[i] = malloc(150*sizeof(char));
    }

    //for(int i = 0; i<50; i++) {
    //  ordered[i] = malloc(150*sizeof(char));
    //}

    strcpy(unordered[0], "ba");
    strcpy(unordered[1], "ab");
    strcpy(unordered[2], "aa");

    printf("The list of strings without alphabetical ordering:\n\n");
    for (int i = 0; i<3; i++) {
        printf("This is string %d: %s\n", i, unordered[i]);
    }

    //arrange strings to alphabetical ordering
    char** ordered = a_to_z_ordering(unordered, 3); //3 elements + \0?

    printf("\n\nThe list of strings WITH alphabetical ordering:\n\n");
    for (int i = 0; i<3; i++) {
        printf("This is string %d: %s\n", i, ordered[i]);
    }

    return 0;
}

char** a_to_z_ordering (char** arrayOfStrings, int arraySize) {
    char* tempString = malloc(150*sizeof(char));

    arrayOfStrings = malloc(50*150);
    for(int i = 0; i<arraySize; i++) {
        arrayOfStrings[i] = malloc(150*sizeof(char));
    }

    for(size_t i = 1; i < arraySize; i++) {
        for(size_t n = 1; n <arraySize; n++) {
            if(strcmp(arrayOfStrings[n-1], arrayOfStrings[n]) > 0) {
                strcpy(tempString, arrayOfStrings[n-1]);
                strcpy(arrayOfStrings[n-1], arrayOfStrings[n]);
                strcpy(arrayOfStrings[n], tempString);
            }
        }
    }
    return arrayOfStrings;
}
6
  • 1
    unordered = malloc(50*150); totally wrong. You are allocating an array of 50 char pointers, the correct expression is unordered = malloc(50*sizeof(char*)); Commented Oct 16, 2016 at 20:58
  • You are tryong to sort an array of 50 strings but you have initialized only 3 array elements. Elements from 3 to 49 contain unpredictable garbage that may or may not be null-terminated strings. Commented Oct 16, 2016 at 21:01
  • @user3121023 You are right! Thank you! Why don't you write your comment as an answer, so I can accept it? Commented Oct 16, 2016 at 21:24
  • 1
    Why can't you just swap the values of the char*'s in your sorting procedure? Commented Oct 16, 2016 at 21:30
  • @IatropoulosGeorge do you have something against qsort? It is orders of magnitude faster than your manual sort and your compare function for qsort (the last parameter) can simply be written to sort ascending/descending, etc., however you choose. In your case, you are rewriting a complete sort routine where you would only need to write a compare function to use qsort (not to mention that qsort has been developed and validated with years of testing -- where your sort function may not be quite as -- proven, shall we say) Commented Oct 17, 2016 at 4:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.