0

I have this pointer to char with a collection of c-strings

d|o|g|\0|g|h|o|s|t|\0|r|e|a|p|e|r|\0

I am using qsort() method from c library O want to sort it ascending order but I am comfused on how to short this by string not by char.

I have a char pointer that points to the start of the orginal pointer but what i need is to sort the original char pointer not the new char* and still sort the new char*. without having dangling pointers

3 Answers 3

2

You need to create an array of char* to the start of each string in the original array. then you can sort the new array of char* by whatever sorting function you need.

Here is a sample of what this would look like.

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

size_t multi_string_count(char* str) {
    size_t count = 0;
    while(*str) {
        ++count;
        str += strlen(str) + 1;
    }
    return count;
}

char** split_multi_string(char* str, size_t* numberOfStrings) {
    size_t i;
    char** strArray;

    *numberOfStrings = multi_string_count(str);
    // allocate a new block of memory to hold the array
    strArray = (char**)malloc(*numberOfStrings * sizeof(char*));

    for(i=0; i < *numberOfStrings; ++i) {
        // store the starting address of the string into the array
        strArray[i] = str;
        str += strlen(str) + 1;
    }
    return strArray;
}

int my_comp (const void* p1, const void* p2) {
    return strcmp((const char*)p1, (const char*)p2);
}

int main(int argc, char* argv[]) {
    size_t i, arrayLength;
    char** strArray;
    char* multiStr = "dog\0ghost\0reaper\0cat\0";

    strArray = split_multi_string(multiStr, &arrayLength);
    // Sort the array of pointers by the comparitor function
    qsort(strArray, arrayLength, sizeof(char*), my_comp);

    for(i=0; i < arrayLength; ++i)
        printf("%s, ", strArray[i]);

    free(strArray); // Free up the dynamically allocated array.
    return 0;
}
Sign up to request clarification or add additional context in comments.

3 Comments

I have a char pointer that points to the start of the strins of the orginal pointer but what i need is to sort the original char pointer not the new char* and still sort the new char*
@user3444777: If I understand you, you want to sort your char array of null terminated strings in-place. qsort() can't do that, and it's not obvious to me how you'd do it even with your own algorithm.
@user3444777 You have a block of memory that has a group of strings allocated within it. What I suggested to you is to create a new array of char* (i.e. char** is the same as char*[]) and copy the address of the begining of each string into this new array. This will not modify the original block of memory just point to it. This new array of pointers to strings can be then sorted. Now the new array of pointers will be rearanged to match the sorted order you desired.
0

I think this what you are looking for.

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

int stringCompare(const void* p1, const void* p2)
{
   char** lhs = (char**)p1;
   char** rhs = (char**)p2;
   return strcmp(*lhs, *rhs);
}

int main()
{
   char s1[] = {'d', 'o', 'g', '\0', 'g', 'h', 'o', 's', 't', '\0', 'r', 'e', 'a', 'p', 'e', 'r', '\0'};
   char* s2[] = {s1, s1+4, s1+10};

   qsort(s2, 3, sizeof(char*), stringCompare);
   printf("%s\n", s2[0]);
   printf("%s\n", s2[1]);
   printf("%s\n", s2[2]);
};

8 Comments

This is a spelled-out version of NickC's answer. I don't know why you need stringCompare. Couldn't you just pass strcmp directly to qsort?
And this is a C question. What's up with iostream?
Ah, you can't pass strcmp directly because it uses char* and qsort needs void*. Blech.
@Fred if i tried that would the orinal array changes its nemory locations?
@FredLarson Thanks for pointing out the C question part. I've updated my answer.
|
0

Below is a sample program which you can refer to

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

int compare (const char* a, const char* b)
{
  return strcmp(a, b);
}

int main(void) {
    char *ar[3] = {"dog", "cat", "pig"};
    int i;
    qsort(ar, 3, sizeof(char*), compare); // here 3 is the number of elements in array
    printf("Data after sorting\n");
    for(i=0; i<3; i++)
        printf("%s\n", ar[i]);
    return 0;
}

Here i have initialized the array during declaration, but you can initialize it later also.

1 Comment

you should be using sizeof (char *) rather than 4

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.