c语言 - 我不断遇到分段错误,但找不到它!我认为我将其缩小到一个特定的功能



我说它一定是这个函数,因为它在我输入int后立即停止,并且它不读取print语句。

recipe** readAllRecipes(int numRecipes)
{
recipe** theRecipes = malloc(sizeof(recipe *) * numRecipes);
int i;
for(i = 0; i < numRecipes; i++)
{
scanf("%d", &theRecipes[i]->numItems);

printf("nntt here in readAll for loopn");

theRecipes[i] = readRecipe(theRecipes[i]->numItems);
}
return theRecipes;
}

问题来了:

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

theRecipise[i]未初始化,解引用。应该先分配它:

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

但下面我对此感到困惑:

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

最新更新