1. ## qsort

I am trying to sort an array of strings (an array of character arrays). It says in qsort's man page that it can sort single dimensional arrays. Is there a way I can use qsort to sort my array? and please give the the function to be used as the forth arg, i can't seem to get it right.

2. I'll assume you mean you're using a two dimensional character array? Yes? Ok, pass the array name as the object to be sorted, and write your sort function so that it copies the contents of one row to a temp variable, copy the other row over top of the first, and then the temp array over the second:

char temp[BUFSIZ];

strcpy( temp, array[x] );
strcpy( array[x], array[y] );
strcpy( array[y] temp );

Something similar to the above. However, the ideal way to do it, would be to just use an array of pointers to characters. That is to say:

char (*array)[ELEMENTS];

You then have an array of pointers, which you manipulate. Instead of actually moving the contents of the string, you just shuffle the pointers around.

You could even use this "index" to be considered your sorted list, when in fact, you have your original array/2d array unchanged.

Quzah.

3. I was hoping I could use the qsort() function because I want to learn how to use it.

4. Originally posted by chrismiceli
I was hoping I could use the qsort() function because I want to learn how to use it.
Right, I was telling you how to use Qsort. Use the second of my two examples. Ignore my first method. I was getting myself mixed up on the comparison function, thinking you passed a sort instead.

Something like:
Code:
```char mystrings[STRINGS][LENGTH];
char (*index)[STRINGS];
int x;

for( x = 0; X < STRINGS; x++ )
index[x] = mystrings[x];```
You now have an index which you can pass to qsort. Write a function to compare the contents of what's being pointed at. Something like strcmp.

Quzah.

5. I am lost, here is what I have so far, I think I am way off base though
Code:
```/* the qsort call */

qsort(names, 12, (sizeof (char)) * 12, comp);

/* the names array looks like this */
char names[12][20];
...

/* the comp func */
int comp(char *str1, char *str2) {
}```
the compiler won't compile that saying incompatible pointer types.

6. Take a look at the above code. It's much simpler to sort an index of pointers than it is to sort a two dimensional array.

Quzah.

7. i get incompatible types in assigment errors, here is my code

Code:
```char (*index)[20];
short x;
for(x=0;x<12;x++) {
index[x] = seats[x].name.last;
}```
seats is an array of structures, there is 12 structures also.

8. Yeah, give me a second. I'm about out the door. I did my assignment wrong. (I'll blame the phone.)

Don't mind me. Remove the parenthesis. That give you a pointer to an array of characters. For some reason I got it into my head that I needed parenthesis there. I'm not sure why. Heh.
[/edit]

Quzah.

9. here is what I have so far, can someone help, I get this error using gcc

plane.c:98: warning: passing arg 4 of `qsort' from incompatible pointer type

here is the code
Code:
```void showasa(void) {
char *index[12];
short x;

for(x=0;x<12;x++) {
index[x] = seats[x].name.last;
}
qsort(index, 12, sizeof(char *), comp);
}

/* seats is an array of structures with a structure with a  string in it */

/*start of a sorting func */
int comp(char *str1[20], char *str2[20]) {
}```

10. thanx, now I am going to try and reword it with pointers, I really appreciate your help.

11. This is my attempt using pointers, The way you told me worked, i just want to get it to work this way. I think the prob is with my compar function, it compiles but I get output

miceli
ZY\$�

monroe
�EE<fu�E�E}wʋU�

here is my compar function
Code:
```int comp(const void *a, const void *b) {
char *pa, *pb;
pa = (char *) a;
pb = (char *) b;
return strcmp(pa, pb);
}```
here is my function that calls qsort

Code:
```void showasa(void) {
char *index[20];
short x;

for(x=0;x<12;x++) {
*(index+x) = seats[x].name.last;
}
qsort(index, sizeof(index) / sizeof(index[0]), sizeof(index[0]), comp);
for(x=0;x<12;x++) {
printf("%s\n",index[x]);
}
putchar('\n');
}```
I really appreciate your help so far.

12. I must be dense, everything i try gets seg fault. here is my current code, hopefully with the revisions in the right direction.

Code:
```void showasa(void) {
char *index[12];
short x, count;

for(x=0;x<12;x++) {
*(index+x) = seats[x].name.last;
}
*/ --> seg fault here */      qsort(index, sizeof(index) / sizeof(index[0]), sizeof(index[0]), comp);
for(x=0;x<count;x++) {
printf("%s\n",*(index + x));
}
putchar('\n');
}

int comp(const void *a, const void *b) {
char **pa, **pb;
*pa = (char *) a;
*pb = (char *) b;
return strcmp(*pa, *pb);
}```

13. seats[x].name.last is an array of structures with a structure inside of it with the array of chars that make up the last name of a person.

14. Thank you!!!! I owe you big man, I really appreciate your help and tolerance.

<edit> if you couldn't guess I got it working.</edit>