-1

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);
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
z5678
  • 3
  • 2

1 Answers1

1

In scanf you need to pass pointrer to the object you want assign the scanned value

scanf("%c",userInput); -> scanf("%c",&userInput);

And in many other places in your code. And it is the most likely the source of your problems

You need also to declare types of parameters otherwise they are assumed as integers.

Read the warnings as you have for sure uncountable number. You have so many issues here - almost every single line is wrong. So start from the warnings

0___________
  • 60,014
  • 4
  • 34
  • 74