开发者

storing of strings in char arrays in C

#include<stdio.h>
int main()
{
  char a[5]="hello";
  puts(a);  //prints 开发者_JS百科hello
}

Why does the code compile correctly? We need six places to store "hello", correct?


The C compiler will let you run off the end of arrays, it does no checks of that sort.


The C compiler allows you to explicitly ask for no null terminator.

char a[] = "Hello";  /* adds a terminator implicitly */
char a[6] = "Hello"; /* adds a terminator implicitly */
char a[5] = "Hello"; /* skips it */

Any value smaller than 5 results in an error.

As for why - one possibility is that your strings are of a fixed size, or are being used as buffers of byte values. In these cases you do not need a null terminator.

Best practice is to use char a[] so the compiler can set it to the correct value (including terminator) automatically.


a doesn't contain a null terminated string (extra initializers for fixed size arrays - such as the null terminator in "hello" - are discarded), so the behaviour when a pointer to that array is passed to puts is undefined.


In my experience, a lot of compilers will let you get away with compiling this. It will usually crash at runtime, though (because you don't have a null terminator).


C char array initialization includes the terminating null only if there is room or if the array dimensions are not specified.


You need 6 characters to store "hello" as a null terminated string. But char arrays are not constrained to store nul terminated string, you may need the array for another purpose and forcing an additional nul character in those cases would be pointless.


That is because in C memory management is done manually unlike in java and some other few languages.... The six places you allocated is not checked for during compilation but if you have to get into filing(I mean storing actually) you are going to have a runtime error becuase the program kept five places in memory(but is expected to hold six) for the characters but the compiler did not check!


"hello" string is kept in read-only memory with 0 in the end. "a" points to this string, this is why the program may work correctly. But I think that generally this is undefined behavior. It is necessary to see Assembly code generated by compiler to see what happens exactly. If you want to get junk output in this situation, try:

char a[5] = {'h', 'e', 'l', 'l', 'o'}


The C compiler you are using does not check that the string literal fits to the char array. You need 6 characters in the array to fit the literal "Hello" since the literal includes a terminating zero. Modern compilers, such as Visual C++ 2010 do check these things and give you and error.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜