在C中连接字符串包括不需要的字符。

huangapple go评论63阅读模式
英文:

Concatenating strings in C includes unwanted chars

问题

I created a program to concatenate two strings. However, I get some extra characters included in the output string when the char array is too small.

Faulty code below:

char c[3] = "abc";
char d[3] = "def";
char result[8];

strcpy(result, c);
strcat(result, d);

printf("%s\n", result);

Prints abc{def�

Running the program without recompiling gives random chars each time. E.g abc�tdef�.

Changing the size of the result variable makes no difference:

char c[3] = "abc";
char d[3] = "def";
char result[6];

gives abcrdef�.

I know that I need to change the size of the char arrays to c[4] to produce expected output. This is to have space for null termination?

Furthermore, why are those symbols added?
What decides why exactly those chars were included when they appear to be randomly selected?

Why does the result array accept more chars than written? E.g char result[1] gives abcdef as output.

I've tried googling for answers.

英文:

I created a program to concatenate two strings. However I get some extra characters included in the output string when the char array is too small.

Faulty code below:

char c[3] = "abc";
char d[3] = "def";
char result[8];

strcpy(result, c);
strcat(result, d);

printf("%s\n, result);

Prints abc{def�

Running the program without recompiling gives random chars each time. E.g abc�tdef� .

Changing the size of the result variable makes no difference:

char c[3] = "abc";
char d[3] = "def";
char result[6];

givesabcrdef�

I know that I need to change the size of the char arrays to 'c[4]' to produce expected output. This is to have space for null termination?

Furthermore, why are those symbols added?
What decides why exactly those chars were included when they appear to be randomly selected?

Why does the result array acceept more chars than written? E.g 'char result[1]' gives 'abcdef' as output.

I've tried googling for answers.

答案1

得分: 1

char[3] 不足以容纳 "abc",需要使用 char[4]。改用以下方式:

char c[] = "abc";

同样适用于 "def"。结果大小将为 7(不是 6 或 8)。

英文:

char[3] is not big enough to hold "abc", it needs to be char[4]. instead do

 char c[] = "abc";

and let the compiler work out the size for you, same for "def"

the resulting size will be 7 (not 6 or 8)

答案2

得分: 0

strcpy(result, c);

上述对strcpy()的调用会引发未定义的行为,因为第二个参数不是以空字符结尾的字符串。

后续对strcat()printf()的调用以类似的方式触发了未定义的行为。

> 我知道我需要将char数组的大小更改为c[4],以产生预期的输出。这是为了留出空间来进行空终止吗?

确实如此。

> 是什么决定了这些字符在看起来被随机选择时为什么会包含在内?

在C语言级别上,没有什么。您的strcpy()strcat()调用越界了数组的边界,结果行为是未定义的。 — John Bollinger

> 为什么result数组接受了比写入的字符更多的字符?例如,'char result[1]'输出'abcdef'。

一旦程序达到未定义行为的状态,就不能再对程序继续执行作出进一步的假设。

> 为了满足strcpy的要求,字符数组可以写成char c[4]="abc\0"还是char c[4]="abc"

不需要显式的空字节。只需:

char c[4] = "abc";

甚至更好的是,让编译器确定长度:

char c[] = "abc";

脚注:

1 — 来自C11标准:

> 可能的未定义行为范围从完全忽略情况并具有不可预测的结果,到在文档化环境中的翻译或程序执行期间表现出特征的被识别的方式,或终止翻译或执行(带有发出诊断消息)。

英文:
strcpy(result, c);

The above call to strcpy() invokes undefined behaviour¹ because the second argument is not a null-terminated string.

The subsequent calls to strcat() and printf() trigger undefined behaviour in a similar way.

> I know that I need to change the size of the char arrays to 'c[4]' to
> produce expected output. This is to have space for null termination?

Indeed.

> What decides why exactly those chars were included when they appear to
> be randomly selected?

At the C-language level, nothing. Your strcpy() and strcat() calls are overrunning the bounds of your arrays, and the resulting behavior is undefined. — John Bollinger

> Why does the result array acceept more chars than written? E.g 'char
> result[1]' gives 'abcdef' as output.

Once a program reaches a state of undefined behaviour, no further assumption about the continuation of the execution of the program can be made.

> To fulfill strcpy requirements the array of chars can then be written
> as char c[4]="abc\0" or char c[4]="abc"?

An explicit null-byte is not required. Simply:

char c[4] = "abc";

Or even better, let the compiler determine the length:

char c[] = "abc";

Footnote:

1 — From the C11 standard:

> Possible undefined behavior ranges from ignoring the situation
> completely with unpredictable results, to behaving during translation
> or program execution in a documented manner characteristic of the
> environment (with or without the issuance of a diagnostic message), to
> terminating a translation or execution (with the issuance of a
> diagnostic message).

huangapple
  • 本文由 发表于 2023年5月13日 21:21:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242948.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定