I am trying to pass a pointer into a function so that I can make use of more functions. I have been using the code below and have been getting segmentation errors whenever I run the program. Where do I need to add pointers to make this work?
I have used things such as FILE** filePointer and &fileName but this still produces a segmentation error.
FILE* filePointer = fopen(fileName, "r");
checkIfPointerIsNull(filePointer);
char output[256];
printQuestion(output,filePointer);
actualAnswer1 = getactualAnswerX(6,output,filePointer);
actualAnswer2 = getactualAnswerX(7,output,filePointer);
actualAnswer3 = getactualAnswerX(8,output,filePointer);
actualAnswer4 = getactualAnswerX(9,output,filePointer);
userAnswer = getUserInput("word");
int printQuestion(output,filePointer)
{
int i,j;
j = getRandomNumber()%4;
for(i=j;fgets(output, sizeof(output), filePointer) != NULL;i++)
{
if(i<=5)
{
printf("%02d: %c", i, output);
}
}
return 0;
}
char getactualAnswerX(a,output,filePointer)
{
int i;
char actualAnswer1,actualAnswer2,actualAnswer3,actualAnswer4;
for(i=a;fgets(output, sizeof(output), filePointer) != NULL;i++)
{
if(i==6)
{
fscanf(filePointer, "%c", actualAnswer1);
return(actualAnswer1);
}
if(i==7)
{
fscanf(filePointer, "%c", actualAnswer2);
return(actualAnswer2);
}
if(i==8)
{
fscanf(filePointer, "%c", actualAnswer3);
return(actualAnswer3);
}
if(i==9)
{
fscanf(filePointer, "%c", actualAnswer4);
return(actualAnswer4);
}
}
fclose(filePointer);
}
int checkIfPointerIsNull(filePointer)
{
if(filePointer==NULL)
{
printf("Can't load questions");
return 1;
}
}
char getUserInput(word)
{
char userInput;
printf("Input");
printf("%c",word);
scanf("%c",userInput);
return(userInput);
}