我一直遇到分段错误,但我找不到它!我想我已经把它缩小到一个特定的功能
I keep getting a segmentation fault and I can't find it! I think ive narrowed it down to a particular function
我说肯定是这个函数,因为我输入一个int它就停止了,它不读取打印语句
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;
}
这是问题所在:
scanf("%d", &theRecipes[i]->numItems);
theRecipise[i]
未初始化,您取消引用它。应该先分配它:
theRecipes[i] = malloc(sizeof(recipe));
scanf("%d", &theRecipes[i]->numItems);
但低下我对此感到困惑:
theRecipes[i] = readRecipe(theRecipes[i]->numItems);
我说肯定是这个函数,因为我输入一个int它就停止了,它不读取打印语句
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;
}
这是问题所在:
scanf("%d", &theRecipes[i]->numItems);
theRecipise[i]
未初始化,您取消引用它。应该先分配它:
theRecipes[i] = malloc(sizeof(recipe));
scanf("%d", &theRecipes[i]->numItems);
但低下我对此感到困惑:
theRecipes[i] = readRecipe(theRecipes[i]->numItems);