segmentation fault...:(
some one help me pls..

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
        char a[50];
        char *x[10];
        int i,j,n;
        char *temp;
        printf("enter the no of strings:");
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
                printf("enter the string:");
                scanf("%s",a);
                x[i]=(char *)malloc((strlen(a)+1)*sizeof(char));
                if(x[i]==NULL){
                printf("malloc not done");
                exit(1);}
                strcpy(x[i],a);
        }
        for(i=0;i<n-1;i++)
        {
                for(j=0;j<n-i;j++)
                {
                        if(strcmp(x[j],x[j+1])>0)
                        {
                                strcpy(temp,x[j]);
                                strcpy(x[j],x[j+1]);
                                strcpy(x[j+1],temp);
                        }
        }
}
return 0;

}

segmentation fault...:(
some one help me pls..

Always, always check your compiler output. Does your compiler report any warnings? Mine does: "warning: 'temp' may be used uninitialized in this function" at line 28.

Your code declares temp as a char * , and then tries to strcpy into it without allocating any memory. This is why you get the segfault.

Depending on what how you intended the code to work, there are two possible solutions:

  1. Allocate some reasonably-sized memory for temp so you can strcpy into it.
  2. Don't use strcpy , just swap the pointers.

All else being equal, I'd recommend option (2).

this is the modified one still getting segmentation fault :(...any ideas pls

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void swap(char **,char **);
int main()
{
        char a[50];
        char *x[10];
        int i,j,n;
        printf("enter the no of strings:");
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
                printf("enter the string:");
                scanf("%s",a);
                x[i]=(char *)calloc((strlen(a)+1),sizeof(char));
                if(x[i]==NULL){
                printf("malloc not done");
                exit(1);}
                strcpy(x[i],a);
        }
        for(i=0;i<n-1;i++)
        {
                for(j=0;j<n-i;j++)
                {
                        if(strcmp(x[j],x[j+1])>0)
                        {
                                swap(&x[j],&x[j+1]);
                        }
                }
        }
        for(i=0;i<n;i++)
        {
                printf("%s",x[i]);
        }
return 0;

}
void swap(char **p, char **q) {
 char *tmp;

 tmp = *p;
 *p = *q;
 *q = tmp;
}

Having tested this out, I found that the problem is in the conditional of the inner loop. As it is, it not only is skipping possible reverses because it only goes up to i, it is overrunning the end of the array. A workable solution would be

for(j=0;j < n - 1;j++)

You can also shave some run time off by checking for whether any swaps were made on a given pass; if there weren't any, you have sorted the list and can stop.

void bubblesort(char** list, int length)
{
    int i, j, swapped;

    for (i = 0; i < length - 1; i++)
    {
        swapped = 0;

        for (j = 0; j < length - 1; j++)
        {
            if(strcmp(list[j], list[j+1]) > 0)
            {
                swapped = 1;
                swap(&list[j], &list[j+1]);
            }
        }
        if (!swapped)
        {
            break;    /* no swaps made on last pass, sorted */
        }
    }

   /* printf("\nCompleted in %d passes\n", i); */
}

Oh, as a matter of note: please place code-tags around your code samples in the future. So far, the moderators have been kind enough to supply them for you, but formatting the code yourself would go far in gaining your their goodwill.

Schoil-R-LEA
thanks now its working fine but still what's the problem with the previous one because with integers the previous for loop is working fine

ok now i got it.when i=0,j=0;then its checking with outside element which doesn't exist

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.