开发者

Does a char array need to be one byte bigger than you intend to use? - C

I've started learning C and am a bit confused when it comes to arrays.

#include <stdio.h>

int main()
{
    int i;
    char j[5];

    for (i = 0; i < 5; i++)
    {
        j[i] = 'a';
    }

    printf("%s\n", j);
}

Running this code prints out

aaaaa♣

I've read that the char array needs to be one byte longer than the string so the compiler can place the \0 at the end. If I replace the code with this:

#include <stdio.h>

int main()
{
    int i;
    char j[5];

    for (i = 0; i < 4; i++)
    {
        j[i] = 'a';
    }

    printf("%s\n", j);
}

The output I get is:

aaaaa

The char array is one byte longer than I'm using. I suspect this is why I don't see that odd character at the end of the string?

I tried to test this theory with the following code:

#include <stdio.h>

int main()
{
开发者_运维百科    int i;
    char j[5];

    for (i = 0; i < 4; i++)
    {
        j[i] = 'a';
    }

    for (i = 0; i < 4; i++)
    {
       printf("%d\n", j[i]);
    }
}

But, in the output, I see no nullbyte. Is this because it will only be added when outputed as a string?

97
97
97
97


It's your job to add the null byte. The compiler won't necessarily do it for you. Local variables are generally left uninitialized at runtime.

int i;
char j[5];    /* five uninitialized characters, could be anything */

for (i = 0; i < 4; i++)
{
    j[i] = 'a';
}

j[4] = '\0';  /* explicitly add null terminator */

Notice that if you use a string initializer rather than manually setting each character then the compiler will handle adding the null terminator for you:

char j[5] = "aaaa";  /* initialize to {'a', 'a', 'a', 'a', '\0'} */


In both of these cases you print an array of char without 0-termination using a function that expects 0-termination.

Append a '\0' to each string (excluding the one with 5 chars)

for (i = 0; i < 4; i++)
    {
        j[i] = 'a';
    }
j[4] = '\0';

Also, your array is 5 chars/bytes long, not 6. You can store 4 chars + 0-termination in it, not 5 chars + 0-termination.

For the %d loop, you print each 'a' as an integer. The "%d\n" string you pass to printf is automatically 0-terminated, so the output is 97\n


The char array is one byte longer than I'm using. I suspect this is why I don't see that odd character at the end of the string?

Not really, if you added only 4 elements, the 5th one is there by coincidence. For the 5th character you should have done this j[4] = '\0';, in this case there might have been a \0 value on the next memory address.

But, in the output, I see no nullbyte

That is because you are only printing the first 4 characters (see your loop).


You need to put the null character in yourself when constructing strings like that.

The only time you don't is when you set a string literal...

char *str = "this is a string";

The compiler will pop the \0 at the end for you.

CodePad.org


There is no null byte because you only printed the first four bytes. The null is in the fifth. Also, unless you use a string generated by the compiler (such as "string"), the null byte needs to be set manually. The only reason your code is working now is that the memory where the string is placed just happens to be 0 already. As for requiring the null byte, c strings use it to indicate the end of the string. It will not always be used for arrays, but needs to be there whenever the array is interpretted as a string.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜