I'm filling an array with 40 pseudo-random capital letters and then converting the array into a string by terminating it with a null character at the 41st element, but my program crashes when I do the latter. Here is the code:
char s1[41];
int i;
for (i = 0; i < 41; i++)
{
s1[i] = ((rand() % 26) + 'A');
}
s1[i] = '\0';
puts(s1);
The program runs just fine if I print out each character in the array one at a time without the null assignment statement, but everything fails when the null assignment is included. I am required to convert the array into a string and then use the puts function. Why is this happening? Is a string not simply an array of characters terminated with a '\0' (null)? What is causing this? Is this a compiler error? Attached is a screenshot of the error message.
I am using Microsoft Visual Studio Express 2013, if it matters.
s1[41].i < 41-->i < 40