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?

4

1 回答 1

4

printf()相信应该如下,

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]);

在您的原件printf()中,您尝试names[q][0]使用%s.

于 2013-04-23T01:15:35.960 回答