1

This is one void function from a larger program, with the function of printing a kind of chart. It would first print the header, and then it uses a for loop to print 13 lines that calls information from 4 arrays. Names, a 2D Char Array, Scores, a 2D Int Array, Average, a 1D Float array, and Letter, a 1D Char array.

The function itself looks like this:

void display(char names[][8], int scores[][4], float *average, char *letter)
{
  int q = 0;

  printf("\n\n Name \t E1 \t E2 \t E3 \t E4 \t Avg \t Grade");
  for(q=0; q<13; q++)
  {
    printf("\n %s \t %d \t %d \t %d \t %d \t %.2f \t %c", names[q][0], scores[q][0],
    scores[q][1], scores[q][2], scores[q][3],  average[q], letter[q]);
  }
return;
}

Once I run it, it runs well until the function call for this, and once I get there, I get a segmentation fault. As a reference, this is the function call:

    display(&names, &scores, &average, &letter);

Changing the %s in the printf to a %c stops the segmentation fault but I need it to be printing the string held in the array not just a single character.

So how would I print the string (and the statement as a whole) without a segmentation fault?

1 Answer 1

4

The printf() should be as follows I believe,

printf("\n %s \t %d \t %d \t %d \t %d \t %.2f \t %c", names[q], scores[q][0],
    scores[q][1], scores[q][2], scores[q][3],  average[q], letter[q]);

In your original printf() you were trying to print a single character (names[q][0]) using %s.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.