-5
#include <stdio.h>
#include <string.h>

void bubblesort(int n, char *ptr[]){
    char *tmp;
    for (int i = 0; i < n; i++){    
        for (int j = 0 ; j < n; j++){
            if(strcmp(*(ptr+i) , *(ptr+j))>0){
                strcpy(tmp , *(ptr+i));
                strcpy(*(ptr+i) , *(ptr+j));
                strcpy(*(ptr+j) , tmp);
            }
        }
    }
     //printf("\n",ptr+0,ptr+1,ptr+2,ptr+3,ptr+4,ptr+5,ptr+6,ptr+7,ptr+8,ptr+9); **<= without this line my code doesn't executive**
}

int main()
{
    int n;
    scanf("%d\n", &n);
    char str[10][20];
    for (int i = 0; i < n; i++)
 gets(str[i]);
    char *ptr[10];
    for (int i = 0; i < n; i++)
        ptr[i] = str[i];
    bubblesort(n, ptr);
    for (int i = 0; i < n; i++)
        puts(ptr[i]);
}

C:\Users\kk\Desktop\C>a 10 banana kiwi apple tomato lemon melon pear blueberry strawberry orange

tomato strawberry pear orange melon lemon kiwi blueberry banana apple

C:\Users\kk\Desktop\C>

with out above line

C:\Users\kk\Desktop\C>a 10 banana kiwi apple tomato lemon melon pear blueberry strawberry orange

C:\Users\kk\Desktop\C> `

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
  • `char *tmp;` -- Where does this point to? This is uninitialized, yet you are writing characters to that location, wherever it is. Pointers must point somewhere valid before you can use them like the way you're using `tmp`. – PaulMcKenzie Dec 31 '22 at 05:17
  • 1
    Don't use `gets`. The man page even tells you not to. – Tom Karzes Dec 31 '22 at 05:18
  • 1
    Also, don't write `*(ptr+i)`. Just write `ptr[i]`. Don't fight the language. – Tom Karzes Dec 31 '22 at 05:18
  • @PaulMcKenzie actually I'm not good at C especially pointer. may I ask you how and what can I initialize 'char *tmp;'? I don't know even why I have to initialize 'char *tmp;' – kitewatermelon Dec 31 '22 at 05:23
  • @TomKarzes thank you for your advice but this problem is only about implementing bubblesort, so I can't touch gets which in the main function. – kitewatermelon Dec 31 '22 at 05:27
  • Time to read [Why gets() is so dangerous it should never be used!](https://stackoverflow.com/q/1694036/3422102) – David C. Rankin Dec 31 '22 at 05:27
  • 1
    The point of pointers (in this case, the pointers to strings) is that the objects don't need to be shifted; just rearrange (sort) the pointers in the array... Replace those `strcpy()` calls with swapping pointers (and go read the explanation of the bubble sort algorithm.) – Fe2O3 Dec 31 '22 at 05:54
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Dec 31 '22 at 08:37

1 Answers1

0
  1. The inner loop should use j and j + 1 not i and j, and the condition on j for that loop is incorrect.

  2. As you are operating on pointers to string you can just swap those instead of copying the strings themselves. It's clearer to write that swap() function instead of doing it inline.

  3. Finally, gets() is a not safe to use, and for these types of problems it's much better to hard-code a test case.

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

void swap(char **a, char **b) {
    char *tmp = *a;
    *a = *b;
    *b = tmp;
}

void bubblesort(size_t n, char *ptr[n]) {
    if(n < 2) return;
    for (size_t i = 0; i < n - 1; i++)
        for (size_t j = 0; j < n - i - 1; j++)
            if(strcmp(ptr[j], ptr[j + 1]) > 0)
                swap(&ptr[j], &ptr[j + 1]);
}

int main() {
    char *a[] = {
        "Lorem",
        "ipsum",
        "dolor",
        "sit",
        "amet"
    };
    size_t n = sizeof a / sizeof *a;
    bubblesort(n, a);
    for (int i = 0; i < n; i++)
        puts(a[i]);
}

and here is the output:

Lorem
amet
dolor
ipsum
sit
Allan Wind
  • 23,068
  • 5
  • 28
  • 38