Hi,

This function that I've written works well when compiled under the Turbo C compiler on windows XP but it says Segmentation Fault while it runs on linux after compiling with cc Compiler. I've been trying to think what might be wrong....I'm just wasting time....Please help me solve this bug.

The function gets a reference to the structure of type "book".

Am I doing anything wrong while swapping the strings???
Why should it run Perfect under windows?? Why the ruckuss in Linux?

struct book
{
 char title[30];
 char fname[20];
 char lname[20];
 int year;
 float cost;
};

The function where I'm having problem is this:

void real_sort_display(struct book *cat,int count,int flag)
{
        int a,b;
        char *str3;
        int sortTheStrings = count;
        char *str1;
        int d,f;
        //float k,l;
        char *str2;


    if(flag==1)
     {

             printf("flag 1\n");
        for (a = 0; a <= sortTheStrings-1; a++)
         {
          for (b = 0; b <= sortTheStrings-3; b++)
           {
                   printf("%d\n",b);

         
            if(strcmp(cat[b].title,cat[b+1].title)>0)
              {
                      printf("Now in the for loop\n");
                 strcpy(str1,cat[b].lname);//flagged
                 strcpy(str2,cat[b].fname);
                 strcpy(str3,cat[b].title);
                 d=cat[b].year;
                 f=cat[b].cost;

                 printf("1st Segment %d \n",b);

                 strcpy(cat[b].lname,cat[b+1].lname); //flagged
                 strcpy(cat[b].fname,cat[b+1].fname);
                 strcpy(cat[b].title,cat[b+1].title);
                 cat[b].year=cat[b+1].year;
                 cat[b].cost=cat[b+1].cost;

                 printf("2nd Segment %d \n",b);

                 strcpy(cat[b+1].lname,str1);  //flagged
                 strcpy(cat[b+1].fname,str2);
                 strcpy(cat[b+1].title,str3);
                 cat[b+1].year=d;
                 cat[b+1].cost=f;

                 printf("3rd Segment %d \n",b);


                  }//end of if

           }  //end of inner for loop
         }//end of outer for loop
         //clrscr();
         //fflush(stdout);


 printf("Sorted by Title\n");
           for(a=0;a<=sortTheStrings-1;a++)
            {

   printf(" %s | %s | %s | %d | %f \n",cat[a].title,cat[a].fname,cat[a].lname,cat[a].year,cat[a].cost);
            printf(" ");
            //HR(80);
            }
         } //end of flag==1

 } //end of function real sort display

The function is called this way:

real_sort_display(book_array,bc,flag);

Thank You.

>Why should it run Perfect under windows??
Pure luck.

>Why the ruckuss in Linux?
The problem is here:

strcpy(str1,cat[b].lname);//flagged
strcpy(str2,cat[b].fname);
strcpy(str3,cat[b].title);

str1, str2, and str3 don't point to anywhere predictable, and certainly not to arrays (that you own) large enough to hold the strings your copying into them. You need to allocate memory to these pointers before you can use them as temporary buffers.

Thank you very much for your reply. I did it. I allocated memory and free() ed them at the end of function. Its working perfectly now. Thank you very much.

This might be a bit advanced for your fossil DOS compiler, but your Linux compiler will have no problems.

ANSI-C introduced structure assignments, so your swap can be reduced to.

struct book temp = cat[b];
cat[b] = cat[b+1];
cat[b+1] = temp;
This question has already been answered. Start a new discussion instead.