Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question

1 Answer

up vote 3 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.