You are here:

C/Segmentation fault

Advertisement


Question
I have worked on this for at least 8-10 hours.  The program uses my version of the strcmp() function from the string.h library.  The program takes in ten strings using scanf() and puts them in order using the str_smp() and swap() functions.  This code will compile and execute in the jgrasp environment.  However, it will not run when I am in the unix environment.  Can you find why it works in the jgrasp compiler and I get a segmentation fault in the unix compiler?
Here is the code:

/*    This prgram will read in 10 strings and sort them by my own str_cmp() function. */
  
#include <stdio.h>
#define  N  51            //array length
#define  maxstring  50    //used in my str_cmp function
#define  num_strings  10  //max iteration length for sort (10 strings)

int str_cmp( char string_1[], char string_2[]);
//my str_cmp() function, (strcmp())

void swap_1( char *,  char *);   //prototypes for the sort   
void swap_2( char *,  char *);
void swap_3( char *,  char *);
void swap_4( char *,  char *);
void swap_5( char *,  char *);
void swap_6( char *,  char *);
void swap_7( char *,  char *);
void swap_8( char *,  char *);
void swap_9( char *,  char *);


int main(void)
{

int i=0;
char string_1[N];   //declaration of variables (arrays)
char string_2[N];
char string_3[N];
char string_4[N];
char string_5[N];
char string_6[N];
char string_7[N];
char string_8[N];
char string_9[N];
char string_10[N];

printf("Enter a word.
");  //reads in the strings
scanf("%s", string_1);

printf("Enter a word.
");
scanf("%s", string_2);

printf("Enter a word.
");
scanf("%s", string_3);

printf("Enter a word.
");
scanf("%s", string_4);

printf("Enter a word.
");
scanf("%s", string_5);

printf("Enter a word.
");
scanf("%s", string_6);

printf("Enter a word.
");
scanf("%s", string_7);

printf("Enter a word.
");
scanf("%s", string_8);

printf("Enter a word.
");
scanf("%s", string_9);

printf("Enter a word.
");
scanf("%s", string_10);

for(i=0; i< num_strings; ++i){    //iteration to sort the arrays
if (str_cmp(string_1,string_2) == 1)// 1 means string_1 > string_2, hence
  swap_1(string_1,string_2);       // they are swapped to alphabatize.
if (str_cmp(string_2,string_3) == 1)
  swap_2(string_2,string_3);
if (str_cmp(string_3,string_4) == 1)
  swap_3(string_3,string_4);
if (str_cmp(string_4,string_5) == 1)
  swap_4(string_4,string_5);
if (str_cmp(string_5,string_6) == 1)
  swap_5(string_5,string_6);
if (str_cmp(string_6,string_7) == 1)
  swap_6(string_6,string_7);
if (str_cmp(string_7,string_8) == 1)
  swap_7(string_7,string_8);
if (str_cmp(string_8,string_9) == 1)
  swap_8(string_8,string_9);
if (str_cmp(string_9,string_10) == 1)
  swap_9(string_9,string_10);}

printf("1. %-15s2. %-15s3. %-15s4. %-15s5. %-15s
6. %-15s7. %-15s8. %-15s9. %-15s10. %-15s
",
      string_1, string_2, string_3, string_4, string_5, string_6,
      string_7, string_8, string_9, string_10);
                             


return 0;
}

       //my str_cmp function!
int str_cmp( char string_1[], char string_2[]){
  int i, x=0;
     for (i=0; i<=maxstring; ++i){//compares arrays, letter by letter
        if (string_1[i]<string_2[i]){
        x=-1;       //-1 returned if str1<str2
        return x;}
        else if (string_1[i]>string_2[i]){//1 is retuned if str1>str2
        x=1;
        return x;}}
     if (i=maxstring)//once the max array size has been reached, the
        return x;  // arrays must be equal, hence the 0 is returned
}              


//functions for sorting arrays
void swap_1(char string_1[], char string_2[]){
  int i;                  
  char tmp[i];
  
  for(i=0; i<N; ++i)
  tmp[i] = 0;       
  for (i=0; i<N; ++i){      //series of swaps sort the arrays
  tmp[i] = string_1[i];      //swaps the arrays
  string_1[i] = string_2[i];
  string_2[i] = tmp[i];}
}
  
void swap_2( char string_2[],  char string_3[]){
  int i;
  char tmp[i];
  
  for(i=0; i<N; ++i)
  tmp[i] = 0;
  for (i=0; i<N; ++i){
  tmp[i] = string_2[i];
  string_2[i] = string_3[i];
  string_3[i] = tmp[i];}
}

void swap_3( char string_3[],  char string_4[]){
  int i;
  char tmp[i];
  
  for(i=0; i<N; ++i)
  tmp[i] = 0;
  for (i=0; i<N; ++i){
  tmp[i] = string_3[i];
  string_3[i] = string_4[i];
  string_4[i] = tmp[i];}
}
  
void swap_4( char string_4[], char string_5[]){
  int i;
  char tmp[i];
  
  for(i=0; i<N; ++i)
  tmp[i] = 0;
  for (i=0; i<N; ++i){
  tmp[i] = string_4[i];
  string_4[i] = string_5[i];
  string_5[i] = tmp[i];}
}
  
void swap_5( char string_5[], char string_6[]){
  int i;
  char tmp[i];
  
  for(i=0; i<N; ++i)
  tmp[i] = 0;
  for (i=0; i<N; ++i){
  tmp[i] = string_5[i];
  string_5[i] = string_6[i];
  string_6[i] = tmp[i];}
}
  
void swap_6( char string_6[], char string_7[]){
  int i;
  char tmp[i];
  
  for(i=0; i<N; ++i)
  tmp[i] = 0;
  for (i=0; i<N; ++i){
  tmp[i] = string_6[i];
  string_6[i] = string_7[i];
  string_7[i] = tmp[i];}
}

void swap_7( char string_7[], char string_8[]){
  int i;
  char tmp[i];
  
  for(i=0; i<N; ++i)
  tmp[i] = 0;
  for (i=0; i<N; ++i){
  tmp[i] = string_7[i];
  string_7[i] = string_8[i];
  string_8[i] = tmp[i];}
}
  
void swap_8( char string_8[], char string_9[]){
  int i;
  char tmp[i];
  
  for(i=0; i<N; ++i)
  tmp[i] = 0;
  for (i=0; i<N; ++i){
  tmp[i] = string_8[i];
  string_8[i] = string_9[i];
  string_9[i] = tmp[i];}
}
  
void swap_9( char string_9[], char string_10[]){
  int i;
  char tmp[i];
  
  for(i=0; i<N; ++i)
  tmp[i] = 0;
  for (i=0; i<N; ++i){
  tmp[i] = string_9[i];
  string_9[i] = string_10[i];
  string_10[i] = tmp[i];}
}


Answer
Change:
if (i=maxstring)
To:
if (i==maxstring)

And why do you need so many swap functions?
You can use a single swap fuction and call it many times.
All the swap_?() functions are doing the same thing.
Remove all of them except one.
And call this with different arguments.

I exectued this code on Sun Solaris (Unix) machine and it didn't give segmentation fault.

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.