Continuing from my comment, you are simply not providing adequate space to create character strings in your initializations. The following creates character arrays without space for the nul-terminating character:
char c[3] = "abc";
char a[6] = "Shamal";
You will not receive any warnings during compilation because the initializations are valid initialization for character arrays -- not strings.
For example, char c[3] = "abc";
, results in the following sequential bytes in memory:
| a | b | c |
that is a valid character array of 3-elements and each element can be indexed as c[0], c[1], c[2]
. However, to use c
as a string, c
requires the following 4 sequential bytes in memory:
| a | b | c |\0|
To correct the issue and allow the compiler to include the nul-terminating character as part of your initialization, either do NOT provide a size for the array and the compiler will automatically size the array to provide the additional space needed to include the nul-character, e.g.
char c[] = "abc";
char a[] = "Shamal";
Or (more prone to error), provide room for the nul-character yourself, e.g.
char c[4] = "abc";
char a[7] = "Shamal";
Both will result in c
and a
containing the nul-terminating character, allowing each to be used as strings in your code.