# Thread: Sorting Elements in Lexicographical Order

1. ## Sorting Elements in Lexicographical Order

Hey there,

I have a code right here that i worked on with my teacher. I can say that he did most of the work. Basically i'm sorting elements in a dictionary order. The code that i'm posting right now has a few questions and I would appreciate any answers. As i'm new to C i don't understand everything.
Would appreciate any help

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXWORD 50      // max word size

void swap(char **p, char **q)               // Q1: Why do I need a double pointer?
{
char *temp;

temp = *p;
*p = *q;
*q = temp;
}

void sort_words(char *w[], int n)           //Q2:what does the "[]" mean on the w? that there's an array of words?
{
int i,j;

for(i=0;i<n;++i)                        // Q3: Can someone explain me this? what does n and j signify?
for(j=i+1;j<n;++j)                  // Q4:Need explination about that also especially for the "j=i+1"
if(strcmp(w[i], w[j])>0)        // Q5:why does it have to be bigger than 0?
swap(&w[i], &w[j]);         // Swap the pointers
}

void free_array(char**array, int size)      //Freeing all allocated memory in array.--still don't understand why i
{                                           // need a double pointer
int i;

if (array[size]==NULL)                  // Couldn't allocate char array
{
for(i=0; i<size; ++i)
free(array[i]);                 // Q5: So basically if i understand perfectly (and i hope i do)
//      the array is "i" and i'm freeing all the allocated arrays?
return;
}

for(i=0;i<=size;++i)                    // All allocations are successful
free(array[i]);
}

int main()
{
char **w;       //An array of pointers. if i understand this because of the char but still don't understand the **
char word[MAXWORD];     // So this is my string depending on the size which is 50
int size=0;
int i=0;

w = NULL;                               // Q6: why does the w=NULL?
*word = '\0';                           // Q7: What does this mean again please?

printf("\tPlease enter words (or -1 to stop)--\n");
scanf("%s", word);

while (strcmp(word, "-1"))                              // Q8: what does the string copy refer too?
{
w = (char**)realloc(w, (size+1)*sizeof(char*));     // Q9: Please explain me this especially the "(size+1)"
if (w != NULL)                                      // and the double pointer
{
w[size] = (char*)calloc(strlen(word)+1, sizeof(char));      // Q10: does "w[size]" mean the size of the
if(w[size]!=NULL)                                           // array?
strcpy(w[size], word);
else                                            // Failed allocating memory for this word
{
printf("Error allocating memory!");
free_array(w, size);                        // Freeing all memory allocated in array
free(w);                                    // Freeing the array
exit(1);
}
size++;
}
else                                                //Failed allocating memory for this pointer
{
printf("Error allocating memory");
exit(1);
}
scanf("%s", word);                                  // Get the next word
}
sort_words(w, size);                                    //sort the array of words

printf("\nPrinting the sorted words\n");
for(i=0;i<size;++i)                                     //Printing the sorted words
printf("%s\n", w[i]);

free_array(w, size);                                    //Freeing the allocated memory
free(w);
}```

2. // Q1: Why do I need a double pointer?

char ** is not a "double pointer". It's a "pointer to pointer to char". Pointers are needed in this case to swap items. Say you want to swap two ints. You might make a function that accepts two int *s like this:

Code:
`swapint(int *x, int *y);`
Then you can call it like this

Code:
```int a = 5;
int b = 10;
swapint(&a, &b);```
So now if you take it one step further. The type (int *) is just another type. So if you have two int *'s and you want to swap them, you would probably make a function that accept's two int **

Code:
`swapintstar(int **x, int **y);`
etc.

//Q2:what does the "[]" mean on the w? that there's an array of words?

Yes.

// Q5:why does it have to be bigger than 0?

strcmp(a,b) returns negative values to mean a comes before b
strcmp(a,b) returns positive values to mean a comes after b
clearly there's only one possibility left in case a is the same as b

For some of your other questions it sounds like you understand but I think you should work on smaller examples to get the details worked out. For example casting to char ** is not necessary. One way to program effectively is to break down big problems into little ones. You have a lot of questions which is fine, but don't try to get them all at the same time. Instead take one aspect and try to understand just that, then move on, etc.

3. I might reword Q1 in a way I understood it at first

To modify a value of a variable where a function is called, you use a pointer to it.
Code:
```void fun(int *banana)
{
*banana += 1;
}```
If this was not a pointer, banana would be a new different variable that gets incremented and discarded. By making it a pointer to the variable, the variable in the scope above this function (say, main()) is getting modified.

What if you wanted to modify a pointer in main using a function called? You would have to do the same thing. Make a pointer to the pointer (just like making a pointer to a variable in the previous case) and send a pointer to the pointer to the function.