C语言 如何将char/int添加到C中的char数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34055713/
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 12:14:43  来源:igfitidea点击:

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

c

提问by missjohanna

How can I add '.' to the char Array := "Hello World" in C, so I get a char Array: "Hello World." The Question seems simple but I'm struggling.

我怎样才能添加“。” 到 C 中的 char Array := "Hello World",所以我得到一个 char Array:"Hello World。" 这个问题看起来很简单,但我很挣扎。

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. Do I have to convert the tmp to an char array aswell or is there a better solution?

但它不起作用。它向我显示了错误:“传递 'strcat' 的参数 2 使指针来自整数而不进行强制转换”我知道在 C 中,char 也可以被强制转换为 int。我是否还必须将 tmp 转换为字符数组,还是有更好的解决方案?

回答by Fantastic Mr Fox

strcathas the declaration:

strcat有声明:

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

It expects 2 strings. While this compiles:

它需要 2 个字符串。虽然这编译:

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

strcat(str, tmp);

It will cause bad memory issues because strcatis looking for a null terminated cstring. You can do this:

它会导致糟糕的内存问题,因为strcat正在寻找一个空终止的 cstring。你可以这样做:

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] = '
   char *strcat(char *dest, const char *src);
'; } append(str, tmp)

Of course you may also want to check your string size etc to make it memory safe.

当然,您可能还想检查字符串大小等以确保内存安全。

回答by P.P

The error is due the fact that you are passing a wrong to strcat(). Look at strcat()'s prototype:

该错误是由于您将错误传递给strcat(). 看一下strcat()原型:

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

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

But you pass charas the second argument, which is obviously wrong.

但是你char作为第二个参数传递,这显然是错误的。

Use snprintf()instead.

使用snprintf()来代替。

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

// Fill str here

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


As commented by OP:

正如 OP 所评论的:

That was just a example with Hello World to describe the Problem. 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

这只是一个用 Hello World 来描述问题的例子。在我的真实程序中,它必须是空的。程序稍后会填充它。问题仅包含将 char/int 添加到 char Array

In that case, snprintf()can handle it easily to "append" integer types to a char buffer too. The advantage of snprintf()is that it's more flexible to concatenate various types of data into a char buffer.

在这种情况下,snprintf()也可以轻松处理将整数类型“附加”到字符缓冲区。的优点snprintf()是可以更灵活地将各种类型的数据连接到一个字符缓冲区中。

For example to concatenate a string, char and an int:

例如连接字符串、字符和整数:

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

回答by Riccardo Maffei

In C/C++ a string is an array of char terminated with a NULL byte ('\0');

在 C/C++ 中,字符串是一个以 NULL 字节 ( '\0')结尾的字符数组;

  1. Your string str has not been initialized.
  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.
  1. 您的字符串 str 尚未初始化。
  2. 您必须连接字符串,并且您正在尝试将单个字符(没有空字节,因此它不是字符串)连接到字符串。

The code should look like this:

代码应如下所示:

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

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];
strcpy(str, "Hello World"); //here you copy "Hello World" inside the src array 

and should be replaced with

并且应该替换为

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

strcat(str, tmp);

回答by user3629249

Suggest replacing this:

建议更换这个:

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

with this:

有了这个:

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

回答by Shondeslitch

I think you've forgotten initialize your string "str": You need initialize the string before using strcat. And also you need that tmp were a string, not a single char. Try change this:

我想你忘记了初始化你的字符串“str”:你需要在使用 strcat 之前初始化字符串。而且您还需要 tmp 是一个字符串,而不是单个字符。尝试改变这个:

##代码##

for

为了

##代码##