为什么我需要的char [K + 1]而不是CHAR [K]与长度为k的字符串? [英] Why do I need char[k + 1] instead of char[k] for a string with length k?
问题描述
所以我有一个简单的设置code的:
So I have a simple set of code:
#include <stdio.h>
int main()
{
char x[3] = "ABC"; // (*)
puts(x);
return 0;
}
它返回一个奇怪的输出:
It returns a strange output:
ABC¬ a
使用从上面的答案<一个href=\"http://stackoverflow.com/questions/31057309/why-is-there-a-question-mark-at-the-end-of-the-string\">this问题,我发现,当我换 X [3]
到 X [4]
一切都正常运行
Using the top answer from this question, I found that when I change x[3]
to x[4]
everything runs fine.
但是,为什么?我为什么会在 X奇怪的输出[3]
,为什么 X [4]
罚款?
But why? Why do I get a strange output on x[3]
, and why is x[4]
fine?
推荐答案
既然你问为什么这个收益率 ABC -a
,这里是一个解释:你的字符X [3] =ABC
不适合看跌
。 看跌
预计到零结束的字符串。然而,你的 X
基本上是:
Since you've asked "why" this yields ABC -a
, here's an explanation: your char x[3] = "ABC"
isn't well suited for puts
. puts
expects a string terminated by zero. However, your x
is basically:
char x[3] = {'A', 'B', 'C'};
如你所知,有没有办法得到一个(动态)数组的长度:
As you know, there's no way to get the length of a (dynamic) array:
char * allocate(){
return malloc(rand() + 1);
}
char * mem = allocate(); // how large is mem??
有没有办法让你知道它有多长。但是,打印字符串,它不是别的,只是在内存中的字符的连续序列,函数需要知道当字符串(又名字符序列)结束。
There's no way for you to know how long it is. However, to print a string which is nothing else than a continuous sequence of characters in memory, a function needs to know when the string (aka the character sequence) ends.
这就是为什么美国标准code为信息交换(ASCII)和许多其他字符集包含空字符。它基本上字符
值为 0
:
That's why the American Standard Code for Information Interchange (ASCII) and many other character sets contain the null character. It's basically char
with value 0
:
char wrong_abc[3] = {'A', 'B', 'C'}; // when does it end?
char correct_abc[4] = {'A', 'B', 'C', 0 }; // oh, there's a zero!
现在的功能就像看跌
可以简单地检查 0
:
Now functions like puts
can simply check for 0
:
// Simplified, actual "puts" checks for errors and returns
// EOF on error or a non-negative int on succes.
void puts(const char * str){
int i = 0;
while(str[i] != 0){
putchar(str[i]);
i++;
}
putchar('\n');
}
这就是为什么你
- 需要为在该字符序列+1所有字符存储器,
- 得到了一个未定义的行为,当你忘记了
0
。
- need memory for all characters in the character sequence +1,
- get undefined behaviour when you forget the
0
.
看跌期权的执行
上面再也找不到 0
时,不慎留下你自己的内存(或访问其他数据),这通常会导致一个段错误或其他错误(或者更糟的是,没有得到检测相当长的时间,然后产生关键错误)。在这种情况下,实际的行为是不确定的。
The implementation of puts
above would never find 0
and accidentally leave the memory you own (or access other data), which usually leads to a segfault or other errors (or worse, doesn't get detected for a long time and then yields critical errors). The actual behaviour in such a situation is undefined.
注意字符串(例如ABC
)在结束时自动有一个'\\ 0'
。此外,编译器是足够聪明图文字的长度为你,让你可以简单地使用
Note that string literals (e.g. "ABC"
) automatically have a '\0'
at the end. Also, the compiler is smart enough to figure the length of the literal for you, so you can simply use
char x[] = "ABC";
这样,您不必如果你改变了文字后的后顾之忧。
That way, you don't have to worry if you change the literal later.
这篇关于为什么我需要的char [K + 1]而不是CHAR [K]与长度为k的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!