[Solved] Sorting algorithms using void pointers , how can I do it? [closed]


In alphabetic order (or lexicographical order), you need to use the strcmp string function.

Back to the main subject: the idea behind using the void pointer is to create generic code in C.

Assuming you have an array of the type void *, you can assign values to it by simple doing a typecast. However, if you want to assign an int value, you need to assign an int pointer to it:

int x = 3;
void *a = (int*) &x;

Doing that in String is even easier, since its beginning is a pointer to char:

char s[] = "Hello world";
void *a = (char*) s;

When you do strcmp(s1, s2), you get, as return:

  • -1, if s1 is behind s2 in lexicographic order;
  • 1, if s2 is behind s1 in lexicographic order;
  • 0, if s1 and s2 are the same string.

Hope that helps you get started!

solved Sorting algorithms using void pointers , how can I do it? [closed]