0

I have a simple program to input the an array of 5 strings and output them. But the output is some what weird. The following is my code.

#include <stdio.h>

int main()
{
    char a[10][5];
    int i;
    for(i=0; i<5; ++i)
    {
        printf("\nEnter the name of %d st student:", i+1);
        fflush(stdout);
        gets(a[i]);
    }
    for(i=0; i<5; ++i)
    {
        printf("\n%s", a[i]);
        fflush(stdout);
    }
    return 0;
}

I gave the inputs as tom, john, peter, david and alan and I get the following output.

tom
john
peterdavidalan
davidalan
alan

What could be the problem?

3
  • 3
    char a[10][5] has space for 10 strings each up to 4 characters long. Try char a[5][10] = {0}; for 5 strings each up to 9 characters long, initialized to zeros. Commented Jul 26, 2014 at 12:20
  • 1
    Never use gets: It's such a bad flaw, it was deprecated before C11, and banned then. Commented Jul 26, 2014 at 12:37
  • @Deduplicator and yet scanf("%s", buf); remains Commented Jul 26, 2014 at 14:28

2 Answers 2

7

The C array syntax is pretty confusing IMO - it means the opposite of the way I typically read it... and it looks like you made that mistake too.

Your array has ten slots of strings 4 chars each (it isn't 5 because each string in C must end with a 0 character). So when you enter peter, instead of comfortably fitting in a 10 char buffer, it overflows a 5 char buffer, saving over one of the characters saved previously.

Without the 0 terminator, printf will just keep going, and thus write the other names too.

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

5 Comments

The opposite? Reading left-to-right, you get: char elements named a consisting of 10 elements, each in turn consisting of 5 elements. (Good answer nevertheless.)
I read it as "array of 5(array of 10(chars))" which makes sense in my mind because when you index it, you pull off one layer: a[0], to me, is saying take the first element of the outer array.... which would be that array of 5. And a[0][1] just continues it. Right-to-left might sound like the opposite if you're used to reading it the way you did, but to me that's the natural way to read this code, consistent with functions and indexing... of course, since C puts the array size after the variable name instead of with the other type (wtf?!), neither way quite works since that interruption sucks!
ran out of comment space, but my point is mostly just that C array syntax is awful. I'd prefer almost anything else: D's char[10][5] a; groups it all in the way I like... but something like Go's a [5][10]char is also an improvement on C. (I feel that Go has it totally backward and I find it hard to read due to my ingrained habits, but at least the type doesn't have a name stuffed in the middle of it!)
+1 for mentioning \0 terminator absence and printf ongoing :)
@AdamDRuppe declaration mirrors use... s[0] is the first string, s[1] the second string, s[2] the third string, right? So (s[0])[9] is the 10th character in the first string. Just as buf[9] is the 10th character in buf. Bear in mind that it is an "array of arrays" , i.e. char s[5][10] an array of 5 elements, each of which is an array with 10 elements.
2

just change your char a[10][5]; to char a[5][10];

you will have then 5 rows with 10 columns each. current setting of a lets you hold 10 inputs of 4 characters long (since you need a \0 character at the end of string).

C doesn't check for boundaries and multidimensional arrays are stored continguously in memory. therefore, with input longer than 4 characters you are overflowing the current storage for your char array and writing to next row.

Adam D. Ruppe mentioned that printf() will be printing chars to screen until it meets the \0 terminator. see that you are:

  • loosing terminator with "peter" input
  • loosing terminator with "david" input

hence you get the output "peterdavidalan". now i think you can figure out from where "davidalan" output came.

2 Comments

My answer gives a bit more explanation, but this answer is the easy fix to just make it work!
ah yes, haven't seen your answer while i was editing my post:)

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.