简体   繁体   English

如何在C中的char数组中添加char / int?

[英]How to add a char/int to an char array in C?

How can I add '.' 我怎样才能添加'。' to the char Array := "Hello World" in C, so I get a char Array: "Hello World." to char:= C中的“Hello World”,所以我得到一个char数组:“Hello World”。 The Question seems simple but I'm struggling. 这个问题似乎很简单,但我很挣扎。

Tried the following: 尝试以下方法:

char str[1024];
char tmp = '.';

strcat(str, tmp);

But it does not work. 但它不起作用。 It shows me the error: "passing argument 2 of 'strcat' makes pointer from integer without a cast" I know that in C a char can be cast as int aswell. 它向我显示了错误:“传递'strcat'的参数2使得整数指针没有强制转换”我知道在C中char可以转换为int。 Do I have to convert the tmp to an char array aswell or is there a better solution? 我是否必须将tmp转换为char数组,还是有更好的解决方案?

strcat has the declaration: strcat有声明:

char *strcat(char *dest, const char *src)

It expects 2 strings. 它需要2个字符串。 While this compiles: 虽然这编译:

char str[1024] = "Hello World";
char tmp = '.';

strcat(str, tmp);

It will cause bad memory issues because strcat is looking for a null terminated cstring. 它会导致错误的内存问题,因为strcat正在寻找一个空终止的cstring。 You can do this: 你可以这样做:

char str[1024] = "Hello World";
char tmp[2] = ".";

strcat(str, tmp);

Live example. 实例。

If you really want to append a char you will need to make your own function. 如果你真的想追加一个字符,你需要自己创建一个函数。 Something like this: 像这样的东西:

void append(char* s, char c) {
        int len = strlen(s);
        s[len] = c;
        s[len+1] = '\0';
}

append(str, tmp)

Of course you may also want to check your string size etc to make it memory safe. 当然,您可能还需要检查字符串大小等,以确保内存安全。

The error is due the fact that you are passing a wrong to strcat() . 错误是由于您将错误传递给strcat() Look at strcat() 's prototype: 看看strcat()的原型:

   char *strcat(char *dest, const char *src);

But you pass char as the second argument, which is obviously wrong. 但是你传递char作为第二个参数,这显然是错误的。

Use snprintf() instead. 请改用snprintf()

char str[1024] = "Hello World";
char tmp = '.';
size_t len = strlen(str);

snprintf(str + len, sizeof str - len, "%c", tmp);

As commented by OP: 由OP评论:

That was just a example with Hello World to describe the Problem. 这只是Hello World描述问题的一个例子。 It must be empty as first in my real program. 在我的真实程序中它必须是空的。 Program will fill it later. 程序将在稍后填写。 The problem just contains to add a char/int to an char Array 问题只是包含将char / int添加到char数组

In that case, snprintf() can handle it easily to "append" integer types to a char buffer too. 在这种情况下, snprintf()可以轻松处理它,以便将整数类型“追加”到char缓冲区。 The advantage of snprintf() is that it's more flexible to concatenate various types of data into a char buffer. snprintf()的优点是可以更灵活地将各种类型的数据连接到char缓冲区。

For example to concatenate a string, char and an int: 例如,连接字符串,char和int:

char str[1024];
ch tmp = '.';
int i = 5;

// Fill str here

snprintf(str + len, sizeof str - len, "%c%d", str, tmp, i);

In C/C++ a string is an array of char terminated with a NULL byte ( '\\0' ); 在C / C ++中,字符串是以NULL字节( '\\0' )结尾的char数组;

  1. Your string str has not been initialized. 您的字符串str尚未初始化。
  2. You must concatenate strings and you are trying to concatenate a single char (without the null byte so it's not a string) to a string. 您必须连接字符串,并且您尝试将单个字符串(没有空字节,因此它不是字符串)连接到字符串。

The code should look like this: 代码应该如下所示:

char str[1024] = "Hello World"; //this will add all characters and a NULL byte to the array
char tmp[2] = "."; //this is a string with the dot 
strcat(str, tmp);  //here you concatenate the two strings

Note that you can assign a string literal to an array only during its declaration. 请注意,只能在声明过程中将字符串文字指定给数组。
For example the following code is not permitted: 例如,不允许使用以下代码:

char str[1024];
str = "Hello World"; //FORBIDDEN

and should be replaced with 并应替换为

char str[1024];
strcpy(str, "Hello World"); //here you copy "Hello World" inside the src array 

I think you've forgotten initialize your string "str": You need initialize the string before using strcat. 我想你已经忘记了初始化你的字符串“str”:你需要在使用strcat之前初始化字符串。 And also you need that tmp were a string, not a single char. 而且你还需要tmp是一个字符串,而不是一个字符。 Try change this: 尝试改变这个:

char str[1024]; // Only declares size
char tmp = '.';

for 对于

char str[1024] = "Hello World";  //Now you have "Hello World" in str
char tmp[2] = ".";

Suggest replacing this: 建议替换这个:

char str[1024];
char tmp = '.';

strcat(str, tmp);

with this: 有了这个:

char str[1024] = {'\0'}; // set array to initial all NUL bytes
char tmp[] = "."; // create a string for the call to strcat()

strcat(str, tmp); // 

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

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