Thread: Illegal instruction problem with my program

  1. #1
    Registered User
    Join Date
    Apr 2007

    Illegal instruction problem with my program

    Howdy, I'm new to the forum and needed some help. I've put in some printf statements here and there to figure out where the problem is, and it is in the line that is "len=strlen(rtrim(temp));" At that point the program crashes and I cannot quite figure out why. If any of you could help me out I'd really appreciate it. Thank you.

    EDIT: I found a way around the above mentioned problem. But now I am getting a segmentation fault at the qsort. If you could help me with that I'd appreciate it. Thanks.
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h>
      typedef enum{general, home, work, cell, FAX, other} Phone_type;
      typedef struct
        char *name;
        char *number;
        char *ext;
        Phone_type type;
        char *group;
        void *future;
      } Contact; 
    int my_cmp(Contact *s1, Contact *s2)
    char * rtrim(char* str)
    char * ptr;
    ptr = strchr(str, 0) - 1;
    while(ptr >= (char *) str && (isspace(*ptr)))
    *ptr-- = '\0';
    return str;
    int main (int argc, char *argv[])
      int len=0, i=0, length[6], count=0, tempnum=0;
      char *line_ptr, one_line[25], temp[100], line[100];
      FILE *input; FILE *config; FILE *output;
      config=fopen(argv[1], "r");
      for(i=1; i<=6; i++)
          line_ptr=fgets(one_line, 64, config);
          sscanf(one_line,"&#37;d", &tempnum);
          else if (i==2)
          else if (i==3)
          else if (i==4)
          else if (i==5)
          else if (i==6)
      input=fopen(argv[2], "r");
      output=fopen(argv[3], "w");
      for (count=0; 1;count++)
          if(line_ptr==NULL) break;
      int Max_contacts=count;
      Contact cell_list[Max_contacts];
      freopen(argv[1], "r", input);
      for (i=0; i<=count;i++)
          if(line_ptr==NULL) break;
          temp[length[0]] = '\0' ;
          strcpy(cell_list[i].name, temp);
          strncpy(temp, line+length[0], length[1]);
          temp[length[1]] = '\0';
          cell_list[i].number = calloc(len+1,sizeof(char));
          strcpy(cell_list[i].number, temp);
          strncpy(temp, line+length[0]+length[1], length[2]);
          temp[length[2]] = '\0';
          cell_list[i].ext = calloc(len+1,sizeof(char));
          strcpy(cell_list[i].ext, temp);
          strncpy(temp, line+length[0]+length[1]+length[2], length[3]);
          temp[length[3]] = '\0';
          cell_list[i].type = atoi(temp);
          strncpy(temp, line+length[0]+length[1]+length[2]+length[3], length[4]);
          temp[length[4]] = '\0';
          cell_list[i].group = calloc(len+1,sizeof(char));
          strcpy(cell_list[i].group, temp);
          strncpy(temp, line+length[0]+length[1]+length[2]+length[3]+length[4], length[5]);
          temp[length[5]] = '\0';
          cell_list[i].future = (void*)calloc(len+1,sizeof(char));
          strcpy(cell_list[i].future, temp);
      printf("before sort1\n");
      /* Sort by name */
      qsort(cell_list, count, sizeof(Contact), my_cmp);
      printf("before print1[%d]\n", count);
      /* Printing values in xml format */
      for (i=0;i<count;i++)
    	    fprintf(output,"<name>%s</name>\n", cell_list[i].name);
          fprintf(output,"<number>%s</number>\n", cell_list[i].number);
          fprintf(output,"<extension>%s</extension>\n", cell_list[i].ext);
          fprintf(output,"<type>%d</type>\n", cell_list[i].type);
          fprintf(output,"<group>%s</group>\n", cell_list[i].group);
          fprintf(output,"<future>%s</future>\n", (char*)cell_list[i].future);
      printf("after fclose");   
      return 0;
    Last edited by bigtruckguy3500; 04-16-2007 at 10:21 PM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    You are never allocating memory to temp before you start using it. Remember, it's just a pointer, but you have to set where it points to before you use it, or suffer problems like this.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Quote Originally Posted by MacGyver View Post
    You are never allocating memory to temp before you start using it. Remember, it's just a pointer, but you have to set where it points to before you use it, or suffer problems like this.
    Thanks man, I fixed that problem. But now I've got a segmentation fault with the qsort function. Can you see what's wrong with it please. thanks.

    EDIT:Ok, it's not just the qsort that's giving me a segmentation fault. qsort does it, as well as something below it.
    Last edited by bigtruckguy3500; 04-16-2007 at 10:27 PM.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    United States
    Did you mean something like this? If you don't give qsort the correct parameters, it should fault, like you're experiencing
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    struct contact {
        char name[BUFSIZ];
        char phonenumber[13];
    int comp( const void* a, const void* b ) {
        const struct contact* pa = a, *pb = b;
        return strcmp( pa->name, pb->name );
    int main( void ) {
        struct contact array[] =
            { "John Q. Public", "555-555-5555" },
            { "Aaron Smith", "123-456-7899" },
            { "John Doe", "555-666-7777" },
            { "Ken Kennedy", "810-777-5689" },
        qsort( array, sizeof array / sizeof array[0], sizeof array[0], comp );
        return 0;
    Not exactly the same, but close enough.

    Fair warning, I can barely read what you've posted. Whitespace is your friend, but you use it in some odd places, and there are others that could use spacing out. Follow the example of those before you, and achieve satori.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simulator
    By MasterAchilles in forum C Programming
    Replies: 10
    Last Post: 11-30-2008, 10:31 PM
  2. problem w/ doubles in friend's program
    By mkylman in forum C Programming
    Replies: 16
    Last Post: 11-22-2008, 10:45 AM
  3. I have finished my program, one problem
    By sloopy in forum C Programming
    Replies: 4
    Last Post: 11-29-2005, 02:10 AM
  4. Program problem
    By Birdhaus in forum C++ Programming
    Replies: 6
    Last Post: 11-21-2005, 10:37 PM
  5. Problem with Program not Quitting
    By Unregistered in forum Windows Programming
    Replies: 20
    Last Post: 06-11-2002, 11:06 PM