14

Probably I'm just too dump for googling, but I always thought char arrays get only null terminated by an literal initialization (char x[]="asdf";) and got a bit surprised when I saw that this seems not to be the case.

int main()
{
    char x[2];
    printf("%d", x[2]);
    return 0;
}

Output: 0

Shouldn't an array declared as size=2*char actually get the size of 2 chars? Or am I doing something wrong here? I mean it isn't uncommon to use a char array as a simple char array and not as a string, or is it?

3 Answers 3

28

You are accessing an uninitialized array outside its bounds. That's double undefined behavior, anything could happen, even getting 0 as output.

In answer to your real question: Only string literals get null-terminated, and that means that char x[]="asdf" is an array of 5 elements.

Sign up to request clarification or add additional context in comments.

11 Comments

@user1329846: Whatever is stored in memory after the array ends is up to the compiler, you are getting 0s but could as well get any other number as the standard says if you try to read that location all bets are off. Let me make this clear: even if you were lucky to always get 0s there, your code still triggers undefined behavior and may fail at any time.
Thank you for not using anything can happen, even your computer could set itself on fire. No, it can't. And all those grandiose examples are just stupid.
@skynorth: they're not stupid, just simplified for dramatic effect. The correct version is, "anything is permitted to happen by the standard -- the implementation is permitted to to set the computer on fire". The humorous version simply ignores that there are constraints on the computer's behavior other than the C standard -- OS, hardware, fire safety regulations, laws of physics, etc.
@skynorth: For some people, the fact that something works seems to imply that its standard and correct code. Telling them that it may break tomorrow does not cause any effect since all they care is that it works right now. "It may set your computer on fire", on the other hand, has a way of making them stop and think twice. Maybe they should have majored in Theater instead of Computer Sciences though...
@K-ballo, that is now just a matter of following directions. If my coworkers or boss had to tell me, each time, that my computer could catch fire if I did something against their recommendation (something wrong), I would probably not have a job for long. Maybe they should have majored in Theater since it seems they are not cut out for programming (a field where following established knowledge is everything - and occasional innovation). Not my job to judge though.
|
5

char arrays are not automatically NULL terminated, only string literals, e.g. char *myArr = "string literal";, and some string char pointers returned from stdlib string methods.

C does no bounds checking. So an array declared as size 2*char gives you 2 memory slots that you can use, but you have the freedom to walk all over the memory on either side of that, reading and writing and thereby invoking undefined behavior. You may see 0 bytes there. You may write to array[-1] and crash your program. It is your responsibility to keep track of the size of your array and avoid touching memory that you didn't allocate.

It is common to use a char array as a simple char array, i.e. other than a C string, for instance, to hold any arbitrary buffer of raw bytes.

Comments

1

If t is an array of size 2, then the last case is t[2 - 1] = t[1] and not 2. t[2] is out of bounds.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.