C语言 strcat 将一个字符连接到一个字符串上?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4834811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:39:40  来源:igfitidea点击:

strcat concat a char onto a string?

cstringcharconcatenationstrcat

提问by Blackbinary

Using GDB, I find I get a segmentation fault when I attempt this operation:

使用 GDB,当我尝试此操作时,我发现出现分段错误:

strcat(string,&currentChar);

Given that string is initialized as

鉴于该字符串被初始化为

char * string = "";

and currentChar is

和 currentChar 是

char currentChar = 'B';

Why does this result in a segmentation fault?

为什么这会导致分段错误?

If strcat can't be used for this, how else can I concat a char onto a string?

如果 strcat 不能用于此,我还能如何将字符连接到字符串上?

采纳答案by Elalfer

Because &currentCharis not a string, it doesn't finish with \0character. You should define Bas char *currentChar = 'B';. Also according to http://www.cplusplus.com/reference/clibrary/cstring/strcatstringshould have enough space to hold the result string (2 bytes in this case), but it is only 1 byte.

因为&currentChar不是字符串,所以不以\0字符结尾。您应该定义Bchar *currentChar = 'B';. 同样根据http://www.cplusplus.com/reference/clibrary/cstring/strcatstring应该有足够的空间来保存结果字符串(在这种情况下为 2 个字节),但它只有 1 个字节。

Or if you want to use charthen you can do something like (depending of your code):

或者,如果您想使用,char则可以执行以下操作(取决于您的代码):

char string[256];
...

char currentChar = 'B';
size_t cur_len = strlen(string);
if(cur_len < 254) {
    string[cur_len] = currentChar;
    string[cur_len+1] = '
char cToStr[2];
cToStr[1] = '
char * string = "";
char currentChar = 'B';
';
'; } else printf("Not enough space");

回答by gagallo7

As responded by others, &currentCharis a pointer to charor char*, but a string in C is char[]or const char*.

正如其他人所回应的那样,¤tChar是指向charchar*的指针,但 C 中的字符串是char[]const char*

One way to use strcat to concatenate a charto stringis creating a minimum string and use it to transform a char into string.

使用 strcat 将字符连接到字符串的一种方法是创建一个最小字符串并使用它来将字符转换为字符串。

Example:

例子:

Making a simple string, with only 1 character and the suffix '\0';

制作一个简单的字符串,只有 1 个字符和后缀'\0'

cToStr[0] = currentChar;

Applying to your question:

适用于您的问题:

strcat ( string, cToStr );

cToStr will assume the string "B":

cToStr 将假定字符串“B”:

char string[10] = "";
char* currentChar = "B";
strcat(string, currentChar);

And strcat will work!

并且 strcat 会起作用!

char buf[1024];

strcat(buf, "");
strcat(buf, "B");

回答by Jonathan Wood

strcat() takes two '\0'-terminated strings. When you pass the address of a character, the routine will look at the memory that follows the character, looking for the terminator.

strcat() 需要两个以 '\0' 结尾的字符串。当您传递一个字符的地址时,例程将查看该字符后面的内存,寻找终止符。

Since you don't know what that memory even refers to, you should expect problems when your code accesses it.

由于您甚至不知道该内存指的是什么,因此在您的代码访问它时应该会出现问题。

In addition to that, your string argument does not have room to have any characters appended to it. Where is that memory written to? It will attempt to write past the end of the memory associated with this string.

除此之外,您的字符串参数没有空间可以附加任何字符。那条内存写到哪里去了?它将尝试越过与此字符串关联的内存的末尾进行写入。

回答by Nandakumar Edamana

I think the simplest method (not efficient) would be sprintf

我认为最简单的方法(效率不高)是 sprintf

sprintf(str, "%s%c", str, chr);

sprintf(str, "%s%c", str, chr);

回答by helloworld922

Both of the strings must be null-terminated. A single char isn't null terminated, so it's undefined when strcat will stop concatenating characters to the end. Also, string must contain at least enough space for both the original string and resultant string.

两个字符串都必须以空字符结尾。单个字符不是空终止的,因此当 strcat 停止将字符连接到末尾时它是未定义的。此外,字符串必须至少为原始字符串和结果字符串包含足够的空间。

This works:

这有效:

##代码##

回答by Foo Bah

The first argument of strcat must have enough space to hold the rest of the string. "" is a constant string and as such GCC does not allocate space.

strcat 的第一个参数必须有足够的空间来容纳字符串的其余部分。"" 是一个常量字符串,因此 GCC 不分配空间。

Make it an array with enough space:

使其成为具有足够空间的数组:

##代码##