0

I was given a file with several recipes denoted at the beginning by 0. Each recipe is followed by it's ingredients denoted at the beginning by 1 as shown below :

char *rawRecipes[]={ "0Broccoli Coleslaw", "1olive oil", "1white vinegar", "1white sugar", "1package chicken flavored ramen noodles", "1broccoli", "1carrots", "1green onions", "1sunflower seeds", "0Creamy Broccoli Salad", "1broccoli", "1red onion",

I'm trying to alphabetically sort the ingredients in a list, but I'm getting segmentation fault. I would greatly appreciate your help on this. Here is what I've done:


int main(void)
{
    int input; 
    printf("\n"); 
    printf("\n"); 
    printf("Enter a command by number\n");
    printf("4. List All Ingredients in alphabetical order\n"); 
    printf("Give input: "); 
    scanf("%d", &input);

    if (input == 4) //  List All Ingredients in alphabetical order
    { 
        int i = 0,k;
        char alphabet[1000] ; 
        while(strcmp(rawRecipes[i], "") !=0)
            {
                if(rawRecipes[i][0] == 1 && rawRecipes[i+1][0] == 1)
                {
                    char temp;
                    for(k=0; k<250; k++)
                  { 
                        alphabet[k] = rawRecipes[i]; 
                        alphabet[k + 1] = rawRecipes[i + 1];
                  }
                        if(strcmp(alphabet[i], alphabet[i + 1] > 0))
                            {
                                temp = alphabet[i]; 
                                strcpy(alphabet[i], alphabet[i + 1]); 
                                strcpy(alphabet[i + 1], temp);
                            }
                }
                i++;
            } 

            int m;
            for(m=0; m < 250; m++)
            {
                        printf("%d: %c", m, alphabet[m]); 
            }
    }
    return 0;
}
1
  • Please don't provide a picture of your code (which is useless). Instead, edit your question and paste your entire code into it. Commented Apr 9, 2020 at 23:24

1 Answer 1

0

In this code:

        int i,k;
        char alphabet[1000] ; 
        while(strcmp(rawRecipes[i], "") !=0)

What do you expect the value of i to be?

That value is undefined, and could be 0, -10000, or 10000000. Some of the possible values will cause immediate crash right on the first iteration through the loop, while trying to construct an argument to strcmp (due to i being out of range for access to rawRecipes).

You probably want: int i = 0, k;.

There are other bugs as well.

You should really learn how to use a debugger, and how to debug small programs.

And before debugging, you should turn on compiler warnings, and fix all of the bugs compiler will tell you about.

Here is one from GCC:

t.c:41:37: warning: assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]
   41 |                         alphabet[k] = rawRecipes[i];
Sign up to request clarification or add additional context in comments.

3 Comments

Hi @EmployedRussian, yes I tried fixing them, but I didn't know if what I was doing was correct because of the segmentation fault. I included i =0 in my code and still get segmentation fault.
do you know how I can fix the error that you showed in your comment?
@unicorns101 I do. You need to think about what you are given, and what you are doing. You have set of pointers (to strings), and you need re-order these pointers. Hint: strcpy should not even appear in your solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.