i have declared a char array with space for 5 element as like
char s[5]
if i give input “Hello” for this array then why it does not show error.
Last element is null character but here code run without any error
How many elements can be inserted in char array
@dushyantsingh in C their is no bound checking so it is perfectly possible that we enter extra elements or might while iterating move out of the array, these things might raise error might not raise error.
i am asking about c++
if every char array gets null character in end then how size of 5 elements character array can take “hello” input which is also sizeof 5
@dushyantsingh same thing happens in C++ also, the NULL character which should be at position s[4] is now at s[5] (or some garbage can be at this position as s has been allotted only 5 positions)now this would not lead to immediate error but at some latter stage where we are assuming that at s[4] we will have we would not be having it, also we might run into runtime error also.
Remember the size of array is still 5 its just that the position null is not at its correct place. All this can happen because we do not have any kind of bound checking, this is also a major reason or run time error when size limits are not handled properly.
If this resolves your doubt mark it as resolved.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.
