Thread: help with Segmentation fault (core dumped) output

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    9

    help with Segmentation fault (core dumped) output

    Hi all

    I'm attempting another exercise from Kochan's Programming in C book. It askes to write a function that sorts a dictionary list alphabetically.
    Here's my code:

    Code:
    #include <stdio.h>
    #include <stdbool.h>
    
    struct entry
    {
        char word[15];
        char definition[50];
    };
    
    
    void dictionarySort(struct entry dictionary[], int n)
    {
        int i, j, k, l;
        struct entry temp;
        bool test;
    
        for(k = 0; k < n - 1; ++k)
            for(l = k + 1; k < n; ++l)
            {
             test = true;
                for(i = 0; (dictionary[k].word[i] || dictionary[l].word[i] != '\0') && test; ++i)
                        if(dictionary[k].word[i] > dictionary[l].word[i]) 
                        {
                            for(j = 0; dictionary[k].word[j] != '\0'; ++j)
                                temp.word[j] = dictionary[k].word[j];
    
                            for(j = 0; dictionary[l].word[j] != '\0'; ++j)
                                dictionary[k].word[j] = dictionary[l].word[j];
                            
                            for(j = 0; temp.word[j] != '\0'; ++j)
                                dictionary[l].word[j] = temp.word[j];
                            test = false;
                        }
            }
    }
    
    
    int main(void)
    {
        struct entry dictionary[100] = 
        { { "abyss",    "a bottomless pit"                    },
          { "aardvark", "a burrowing African mammal"        },
          { "acumen",   "mentally sharp; keen"                },
          { "addle",    "to become confused"                },
          { "aerie",    "a high nest"                       },
          { "affix",    "to append; attach"                    },
          { "agar",     "a jelly made from seaweed"            },
          { "ahoy",     "a nautical call of greeting"        },
          { "aigrette", "an ornamental cluster of feathers"    },
          { "ajar",     "partially opened"                    } };
    
        int i;
        void dictionarySort(struct entry dictionary[], int n);
    
        printf("The original, unsorted dictionary:\n");
        for(i = 0; i < 10; ++i)
            printf("%s         %s\n", dictionary[i].word, dictionary[i].definition);
    
        dictionarySort(dictionary, 10);
    
        printf("The sorted dictionary:\n");
        for(i = 0; i < 10; ++i)
            printf("%s         %s\n", dictionary[i].word, dictionary[i].definition);
    
        return 0;
    }
    It's compiling ok, and displays the first call to the printf function from main. However, after this I get the 'Segmentation fault (core dumped)' at the terminal, presumably from the call to the dictionarySort function.
    I had a little look around and found it is something to do with non-existent or not available memory addressing, I think?
    Even so, I can't figure out what is causing the issue in my code. Been driving me round the bend for hours now.
    Any help with what's causing this would be great!
    Cheers
    Dan

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You don't need the function prototype in "main()" on line 53 - delete this line. You already define that function correctly above "main()".

    Your "for()" loop on line 18 seems kind of quirky. Place this line of code immediately after "test = true" on line 20:

    Code:
    printf("k = %d, l = %d\n",k,l);
    See if that gives you a hint to where your problem lies.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You had the start of a good Substitution sort going there. What's with the for loop messing around with the end of string char's (starting at line #21).

    Forget the end of string char's - C will handle those for you. Just dump the for loop, there, and use the if statement for the comparison, and then a swap with the help of a temp struct.

    15 chars is a bit short for words, by the way. That's 14 letters. English has words up to 28 letters (conservative rules), so 29 spaces might be needed.

    The line #18 is standard code, and correct, for a Substitution sort, except for the k < n part. Correct is l < n, of course.
    Last edited by Adak; 10-09-2013 at 07:30 AM.

  4. #4
    Registered User
    Join Date
    Sep 2013
    Posts
    82
    Why don't you use the function qsort?? qsort - C++ Reference

  5. #5
    Registered User
    Join Date
    Sep 2013
    Posts
    9
    Thanks for the input guys. I'm no longer getting the output regarding the core dumped. Changing the k to l on line #18 fixed that (amazing how many times I looked at the code and didn't see this).
    I'm still getting an incorrect output though, but going to fiddle with it with a fresh mind later on.
    There are probably simpler ways of doing this (e.g. qsort?), and the output is somewhat impractical, but the exercise is to just get me to use some of the guts of the C language. Moving on to pointers in the next chapter

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    ^^^ Probably want the sorting to be hand coded.

    Code:
    #include <stdio.h>
    #include <stdbool.h>
    //you'll need to add string.h to the include list
     
    struct entry
    {
        char word[15];
        char definition[50];
    };
     
     
    void dictionarySort(struct entry dictionary[], int n)
    {
        int i,j,k,l; //delete k and l
        struct entry temp;
        bool test;
     
        for(i = 0; i < n - 1; ++i)
            for(j = i + 1; j < n; ++j)
            {
             //test = true;  
                //for(i = 0; (dictionary[k].word[i] || dictionary[l].word[i] != '\0') && test; ++i)
    
                 /* add this */
                  if(strcmp(dictionary[i].word, dictionary[j].word)> 0) {
                      temp = dictionary[i];
                      dictionary[i]= dictionary[j];
                      dictionary[j] = temp;
                }
                /* because in C, you can't compare strings with an equal sign */
                /* use the return value of strcmp(), instead */
    
                /* delete this block of code 
    
                for(j = 0; dictionary[k].word[j] != '\0'; ++j)
                                temp.word[j] = dictionary[k].word[j];
     
                            for(j = 0; dictionary[l].word[j] != '\0'; ++j)
                                dictionary[k].word[j] = dictionary[l].word[j];
                             
                            for(j = 0; temp.word[j] != '\0'; ++j)
                                dictionary[l].word[j] = temp.word[j];
                            test = false;
                        } 
                 it isn't needed. You can swap a struct in total IF the struct 
                 has no pointers in it. 
    
                If it has pointers, then whatever they point to has to be 
                swapped, one item at a time.
    
                   */
            }
    }
      
    int main(void)
    {
        struct entry dictionary[100] =
        { { "abyss",    "a bottomless pit"                    },
          { "aardvark", "a burrowing African mammal"        },
          { "acumen",   "mentally sharp; keen"                },
          { "addle",    "to become confused"                },
          { "aerie",    "a high nest"                       },
          { "affix",    "to append; attach"                    },
          { "agar",     "a jelly made from seaweed"            },
          { "ahoy",     "a nautical call of greeting"        },
          { "aigrette", "an ornamental cluster of feathers"    },
          { "ajar",     "partially opened"                    } };
     
        int i;
        void dictionarySort(struct entry dictionary[], int n);
     
        printf("The original, unsorted dictionary:\n");
        for(i = 0; i < 10; ++i)
            printf("%s         %s\n", dictionary[i].word, dictionary[i].definition);
     
        dictionarySort(dictionary, 10);
     
        printf("The sorted dictionary:\n");
        for(i = 0; i < 10; ++i)
            printf("%s         %s\n", dictionary[i].word, dictionary[i].definition);
     
        return 0;
    }
    Make those changes, and study up!

  7. #7
    Registered User
    Join Date
    Sep 2013
    Posts
    9
    Thanks for the input Adak!
    I went back to the problem and came up with a solution using the knowledge I have at the minute. It was pretty straight forward in the end. I'm going to see how it differs to yours and see what I can take from it.
    Here's my new code in case any one can learn from it.
    Code:
    #include <stdio.h>
    #include <stdbool.h>
    
    
    struct entry
    {
    	char word[15];
    	char definition[50];
    };
    
    
    
    
    void dictionarySort(struct entry dictionary[], int n)
    {
    	int i, j, k, l;
    	struct entry temp;
    
    
    	for(k = 0; k < n - 1; ++k)
    		for(l = k + 1; l < n; ++l)
    		{
    			i = 0;
    			while(dictionary[k].word[i] == dictionary[l].word[i])
    				++i;
    
    
    					if(dictionary[k].word[i] > dictionary[l].word[i]) 
    					{
    						for(j = 0; dictionary[k].word[j] != '\0'; ++j)
    							temp.word[j] = dictionary[k].word[j];
    
    
    						for(j = 0; dictionary[k].definition[j] != '\0'; ++j)
    							temp.definition[j] = dictionary[k].definition[j];						
    
    
    
    
    						for(j = 0; dictionary[l].word[j] != '\0'; ++j)
    							dictionary[k].word[j] = dictionary[l].word[j];
    
    
    						for(j = 0; dictionary[l].definition[j] != '\0'; ++j)
    							dictionary[k].definition[j] = dictionary[l].definition[j];						
    					
    
    
    						for(j = 0; temp.word[j] != '\0'; ++j)
    							dictionary[l].word[j] = temp.word[j];
    
    
    							dictionary[l].word[j - 1] = '\0';
    
    
    						for(j = 0; temp.definition[j] != '\0'; ++j)
    							dictionary[l].definition[j] = temp.definition[j];						
    					
    							dictionary[l].definition[j] = '\0';
    
    
    
    
    					}
    		}
    }
    
    
    
    
    int main(void)
    {
    	struct entry dictionary[100] = 
    	{ { "abyss",    "a bottomless pit"					},
    	  { "aardvark", "a burrowing African mammal"		},
    	  { "acumen",   "mentally sharp; keen"				},
    	  { "addle",    "to become confused"				},
    	  { "aerie",    "a high nest"   					},
    	  { "affix",    "to append; attach"					},
    	  { "agar",     "a jelly made from seaweed"			},
    	  { "ahoy",     "a nautical call of greeting"		},
    	  { "aigrette", "an ornamental cluster of feathers"	},
    	  { "ajar",     "partially opened"					} };
    
    
    	int i;
    	void dictionarySort(struct entry dictionary[], int n);
    
    
    	printf("The original, unsorted dictionary:\n");
    	for(i = 0; i < 10; ++i)
    		printf("%-7s 		%-7s\n", dictionary[i].word, dictionary[i].definition);
    
    
    	dictionarySort(dictionary, 10);
    
    
    	printf("The sorted dictionary:\n");
    	for(i = 0; i < 10; ++i)
    		printf("%-7s 		%-7s\n", dictionary[i].word, dictionary[i].definition);
    
    
    	return 0;
    }
    Thanks again for your input guys.

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if you want to copy whole struct

    Code:
    for(j = 0; dictionary[k].word[j] != '\0'; ++j)
                                temp.word[j] = dictionary[k].word[j];
     
     
                            for(j = 0; dictionary[k].definition[j] != '\0'; ++j)
                                temp.definition[j] = dictionary[k].definition[j];
    why not

    Code:
    temp = dictionary[k];
    It would make a code a lot more readable
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User
    Join Date
    Sep 2013
    Posts
    9
    Thanks Vart, that's much better.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault (core dumped)
    By catasturslykid in forum C Programming
    Replies: 4
    Last Post: 07-29-2013, 08:57 AM
  2. Segmentation Fault (core dumped)
    By Juan Cervantes in forum C Programming
    Replies: 9
    Last Post: 10-29-2012, 05:58 PM
  3. Segmentation fault (core dumped)
    By benjaminp in forum C Programming
    Replies: 8
    Last Post: 10-10-2012, 12:46 PM
  4. Segmentation fault (core dumped)
    By KoRn KloWn in forum C Programming
    Replies: 3
    Last Post: 09-22-2012, 02:34 AM
  5. Segmentation fault (core dumped)????
    By yosipoa in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2011, 01:18 PM