Home > Blockchain >  I keep getting a segmentation fault and I can't find it! I think ive narrowed it down to a part
I keep getting a segmentation fault and I can't find it! I think ive narrowed it down to a part

Time:09-17

I say that it must be this function because it stops right after I enter an int and it doesn't read the print statement.

recipe** readAllRecipes(int numRecipes)
 {
   recipe** theRecipes = malloc(sizeof(recipe *) * numRecipes);
   int i;

   for(i = 0; i < numRecipes; i  )
   {
    scanf("%d", &theRecipes[i]->numItems);
    
    printf("\n\n\t\t here in readAll for loop\n");
    
    theRecipes[i] = readRecipe(theRecipes[i]->numItems);
   }

   return theRecipes;
}

CodePudding user response:

Here's the problem:

        scanf("%d", &theRecipes[i]->numItems);

theRecipise[i] is not initalized and you dereference it. Should allocate it first:

        theRecipes[i] = malloc(sizeof(recipe));
        scanf("%d", &theRecipes[i]->numItems);

but lower down I'm baffled by this:

    theRecipes[i] = readRecipe(theRecipes[i]->numItems);
  • Related