1. That function is doomed to fail because line 23 declared a 3d array of pointers. If what yoiu really want is a 2d character array where each element has room for 20 characters then you need to drop the star and just declare it like this: char wds[20][20];
2. The two loops in your bubble sort are coded wrong. Should be this:
for (pass=0;pass<5;++pass)
{
for(i = pass+1; i < 6; ++i)
{
// code here
}
}
3. you have to use strcmp() to compare the two strings. Your program at line 43 is mearly comparing two pointers, not what they are pointing to.
if( strcmp(wds[pass], wds[i]) > 0)
{
// swap the strings
}
4. You can not swap the two strings like you are attempting to do on line 45 You have to use strcpy().
5. why do you have the second parameter to dsort() ? It is never used so you might as well delete it.
6. Moved this thread to the C board since its a C program, not c++
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
ex printf("%s",string)
scanf("%s",&string)
Those statements are the same becausestring is defined to be a character array and not a pointer The & address operator in front of string is optional because both produce a pointer to the first byte of the string. Both lines below produce identical results.
int main(int argc, char* argv[])
{
char string[] = "Hello World";
printf("%s\n", string);
printf("%s\n", &string);
return 0;
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
It only works because "pointer to whole array - which is &array " and "pointer to first element of array - which is just array " happen to have the same value. The value may be the same, but you're playing with fire with the types.
int main(int argc, char* argv[])
{
char string[] = "Hello World";
char *p = string;
printf("%s\n", string);
printf("%s\n", &string);
printf("%s\n", p); /* still works */
printf("%s\n", &p); /* couldn't be more wrong */
return 0;
}
'gcc -Wall' will warn you if you use &string where string was the correct choice, even it it seems to work for you.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953