0

What does the number means in [ ]

char a[100]; gets(a); I thought the [100] means I could only enter less than 100 characters initially. However, as I set a[10] and entered more than 10 characters, it still showed the result.

30th Mar 2019, 7:54 AM
vance
4 odpowiedzi
+ 4
gets() is obsolete because it is unsafe and does not care about writing to memory beyond array boundaries. Use fgets() instead. https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used
30th Mar 2019, 8:34 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
Seniru Pasan it is not OK exceed the bounds of an array. Technically it results in undefined behaviour. Realistically it will work sometimes, crash sometimes and do strange things sometimes. This is not good in a large project as the bugs can be hard to reproduce and find. Also hackers can use this to get your program to do something it was never supposed to do.
30th Mar 2019, 9:34 AM
Jared Bird
Jared Bird - avatar
+ 2
Jared Bird you are right. What I meant OK here is that you won't get any error from that. I got to know that this will tend to bugs by some of Chill Pill's work. Anyway thanks for your info 👍
30th Mar 2019, 10:12 AM
Seniru
Seniru - avatar
+ 1
It gives a suggestion to the compiler what would be the length of the array going to be. It is OK if we entered more than the length specified. It would be helpful to compiler to be more efficient if we entered a good length
30th Mar 2019, 8:02 AM
Seniru
Seniru - avatar