1

I have the following code which stores string-input from a user N times in a multidimensional array. And then print out the second element.

main()
{

    // Array to store 10 strings, 20 characters long.
    char strStorage[10][20];

    printf("\nEnter how many strings: ");
    scanf( "%d" , &num);
    fflush(stdin);

    for ( count = 0 ; count < num ; count++)
    {
        printf("Enter a string: ");
        gets(strStorage[count]);
        fflush(stdin);
    }

    printf("%s", strStorage[2]);

Last line prints out garbage. The user-input is not visible inside the garbage hence either my element access is wrong or my storage is wrong. Can anyone help me with regards to what is the problem?

Thanks in advance...

kype
  • 555
  • 1
  • 6
  • 24
  • What if the user enters more than 19 characters? http://stackoverflow.com/questions/1694036/why-is-the-gets-function-is-dangerous-why-should-not-be-used – hmjd Feb 13 '13 at 12:28
  • 3
    `fflush(stdin);` is undefined behaviour! – Mats Petersson Feb 13 '13 at 12:28
  • what is your sample input? – Rohan Feb 13 '13 at 12:29
  • If there are 3 strings all shorter than 19 characters, you should see a third string. Consider showing is a compilable example, that is, the code you actually run (and where the problem is still visible). This code isn't. – Anton Kovalenko Feb 13 '13 at 12:30
  • im putting based on the assumption that user will always enter lesser than 20 characters. And since i cant even get this to work, hence i am trying to figure out why ok gime a moment with the code – kype Feb 13 '13 at 12:32
  • @MatsPetersson What do you mean undefined. It flushes out the carraige return character when the user enters a string and presses enter on his keyboard. Am i right? – kype Feb 13 '13 at 12:43
  • According to the C standard, `fflush()` on an input FILE is undefined - it can flush the newline, but it can also cause world-war III to start, if the C library builder chooses to do so - it would probably be a rather foolish thing to do, but it's perfectly within the specs. More likely scenarios are "nothing happens at all" or "program crashes". Don't use `fflush(stdin);` [and don't use `gets()` either - it is completely unsafe!] – Mats Petersson Feb 13 '13 at 12:48

2 Answers2

3

strStorage[2] is the third string, so if num is less than 3, you won't initialize it and it will contain garbage.

kol
  • 27,881
  • 12
  • 83
  • 120
  • Well im inputing 4 strings to test and it was still garbage. Even strStorage[0] doesnt work – kype Feb 13 '13 at 12:39
  • I tested your code in VS, using the debugger, and verified what I claim above. Would you please copy the *whole* program code you use and the 4 strings you typed in when num was 4? I would check it. – kol Feb 13 '13 at 12:46
0

scanf("%d", &num); doesn't ensure that num contains a value. Perhaps it'd be wise to check the return value of scanf to ensure it read 1 value, like this: if (scanf("%d", &num) != 1) { puts("Error reading integer"); }

While we're on this topic, I presume num and count are declared as an int, and you've hidden the declarations from us. Tsssk! Do you want our help? If so, then make your code compilable! Do you really think int is suitable for storing indexes to arrays? It's possible that they might have negative values. I'd suggest using size_t, instead, and the %zu format specifier tells scanf to expect a size_t value from stdin.

... and what happens when that size_t contains a value greater than the number of elements in your array? I suggest researching variable length arrays.

fflush(stdin); is nonsense because fflush defines behaviour for files open for output, and stdin is a file open for input only. That's sort of like flushing the toilet and expecting the waste to come out of the bowl, rather than going down through the S-bend. Perhaps you mean to discard the remainder of a line, because you've read the data you need from the start of it. Something like for (int c = getchar(); c >= 0 && c != '\n'; c = getchar()); might work.

Don't use gets. Use fgets(strStorage[count], sizeof strStorage[count], stdin); instead, to ensure that no buffer overflows occur.

There, I think I covered just about every bit of undefined behaviour and nit-picky stuffs.

autistic
  • 1
  • 3
  • 35
  • 80