简体   繁体   English

在C中将单个字符附加到动态字符数组

[英]Appending a single character to a dynamic character array in C

How do I append a character to a char* ? 如何将字符附加到char* So... 所以...

char* thingy = "test";
char* another = "hello world";

thingy += another[6];

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

I wan't the output to be: 我不要输出是:

testw testw

However, I get this output 但是,我得到这个输出

at address %p 在地址%p

edit: 编辑:

Thanks for the help :) 谢谢您的帮助 :)

There's no string arithmetic in C, so you can't do it that way. C中没有字符串运算,所以您不能那样做。

However, there's strcat() , which you can use (as long as there's room for those characters): 但是,有strcat() ,您可以使用(只要这些字符有空间):

char thingy[256] = "Hello World";

strcat(thingy, "!");

// thingy is now "Hello World!"

Although it's important to note that you should always check string lengths and be careful when doing such things. 尽管需要注意的重要一点是,您应始终检查字符串长度,并在执行此类操作时要小心。

If you'd like to add a single character rather than a string, you can either copy that character to a string: 如果要添加单个字符而不是字符串,则可以将该字符复制到字符串中:

char thingy[256] = "Hello World";

char dummy[] = "#";

dummy[0] = '!';
strcat(thingy, dummy);

// thingy is now "Hello World!"

Or do it the manual way: 或以手动方式执行:

char thingy[256] = "Hello World";

unsigned int len = strlen(thingy);

thingy[len] = '!';      // Append character
thingy[len + 1] = '\0'; // Readd termination

// thingy is now "Hello World!"

thingy is a pointer ( * ) to char , ie, the address of the first character of the string "test" . thingy是指向char的指针( * ),即字符串"test"的第一个字符的地址。 Arithmetic on thingy changes the address to which it points: 关于thingy算法会更改其指向的地址:

thingy += another[6];

This adds the integer value of the char at the address another + 6 to the address pointed to by thingy . 这会将char的整数值在another + 6的地址添加到thingy指向的地址。 This is beyond the end of the string "test" and thus undefined behaviour - it just happens that your program has the string "at address %p" . 这超出了字符串"test"的末尾,从而导致未定义的行为-恰好您的程序具有字符串"at address %p"

Also, the string pointed to by thingy is a constant so you cannot append to it. 另外, thingy指向的字符串是一个常量,因此您无法附加到该常量。 You could make it an array, eg, char thingy[MAX_LENGTH_OF_THINGY] = "test"; 您可以将其设置为数组,例如, char thingy[MAX_LENGTH_OF_THINGY] = "test"; instead, and then do something like thingy[4] = another[6]; thingy[5] = '\\0'; 相反,然后做类似thingy[4] = another[6]; thingy[5] = '\\0'; thingy[4] = another[6]; thingy[5] = '\\0'; (note the need to NUL-terminate C-strings). (请注意需要使用NUL终止的C字符串)。 Or you can create a new string altogether, eg, by malloc ing enough memory and copying the original + the additional character to it. 或者,您可以创建一个全新的字符串,如malloc荷兰国际集团足够的内存和复制原始+添加字符给它。

Your only option in this case is to reallocate the char*'s memory so that you can get a larger string. 在这种情况下,您唯一的选择是重新分配char *的内存,以便获得更大的字符串。

First, you will need the length of the original string, then you must add 1 to it as the strlen function does not include the null terminator: 首先,您将需要原始字符串的长度,然后必须向其添加1,因为strlen函数不包含空终止符:

char* thingy = "test";
char* another = "hello world";

int len = strlen(thingy);
char* thingy = realloc(thingy, (len + 2) * sizeof(char));
thingy[len] = another[6];
thingy[len +1] = '\0';

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

If you have access to C++, however, a better approach is to use the std::string object: 但是,如果可以访问C ++,则更好的方法是使用std :: string对象:

std::string thingy = "test";
std::string another = "hello world";

thingy += another[6];
printf("%s\n", thingy.c_str());

Since strings are containers, there are myriad ways to approach the problem: 由于字符串是容器,因此有多种方法可以解决此问题:

thingy.push_back(another[6]);

thingy.append(another, 6, 1);

thingy.insert(thingy.end(), another[6]);

Another benefit of std strings is that they handle the null terminator for you, so you don't have to worry about it. std字符串的另一个好处是它们为您处理空终止符,因此您不必担心它。

The string you append onto should not be a literal. 您附加到的字符串不应是文字。 If you had: 如果你有:

char thingy[10] = "test";

You can: 您可以:

int len = strlen(thingy);
thingy[len] = another[6];
thingy[len+1] = '\0';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM