将char转换为1个字符的以null结尾的字符串

时间:2021-06-21 20:45:53

Say I have a char like:

说我有一个像:

char a = 'a';

How can I convert it to something like this:

我怎样才能将它转换成这样的东西:

char* b = "a";
// b = {'a', '\0'};

(Technically 2 chars since it should be null terminated)

(技术上2个字符,因为它应该为空终止)

My use case is in a ternary expression, where I want to convert '\0' into "\\0" ({ '\\', '0', \0' }), but every other character will be a letter, which I want to keep the same.

我的用例是三元表达式,我想将'\ 0'转换为“\\ 0”({'\\','0',\ 0'}),但每个其他字符都是一个字母,我想保持不变。

letter == '\0' ? "\0" : letter;

This works, but produces an error about mismatched type. I also have other things that this may need to be used for.

这有效,但会产生关于不匹配类型的错误。我还有其他可能需要使用的东西。

Things I have tried:

我尝试过的事情:

letter == '\0' ? "\\0" : letter;
// error: pointer/integer type mismatch in conditional expression [-Werror]

letter == '\0' ? "\\0" : { letter, '\0' };
//                       ^
// error: expected expression before ‘{’ token

letter == '\0' ? "\\0" : &letter;
// No error, but not null terminated.

letter == '\0' ? "\\0" : (char*) { letter, '\0' };
//                                 ^~~~~~
// error: initialization makes pointer from integer without a cast [-Werror=int-conversion]
// 
// ter == '\0' ? "\\0" : (char*) { letter, '\0' };
//                                         ^~~~
// error: excess elements in scalar initializer [-Werror]
// Seems to want to initialise a char* from just the first thing in the list

char string[2] = {letter, 0};
letter == '\0' ? "\\0" : string;
// Makes a string even if it is `'\0'` already. Also requires multiple statements.

char string[2];
letter == '\0' ? "\\0" : (string = {letter, 0});
//                                 ^
// error: expected expression before ‘{’ token

2 个解决方案

#1


5  

The shortest

char c = 'a';
char s[2] = {c};  /* Will be 0-terminated implicitly */

puts(s);

prints:

a

And if it's just about being able to pass the character to puts() (or alike) you can even use a compound literal

如果它只是能够将角色传递给puts()(或类似),你甚至可以使用复合文字

puts((char[2]){c});

or

{
  puts((char[2]){c});
}

with the latter releasing the memory used by the compound literal immediately.

后者立即释放复合文字使用的内存。

Both print

a

as well.

#2


0  

char str[2] = "\0";
str[0] = c;

and you are good to go.

你很高兴。

Or of course, if this is a harcoded value, then you could do:

或者当然,如果这是一个带符号的值,那么你可以这样做:

char str[2] = "a";

#1


5  

The shortest

char c = 'a';
char s[2] = {c};  /* Will be 0-terminated implicitly */

puts(s);

prints:

a

And if it's just about being able to pass the character to puts() (or alike) you can even use a compound literal

如果它只是能够将角色传递给puts()(或类似),你甚至可以使用复合文字

puts((char[2]){c});

or

{
  puts((char[2]){c});
}

with the latter releasing the memory used by the compound literal immediately.

后者立即释放复合文字使用的内存。

Both print

a

as well.

#2


0  

char str[2] = "\0";
str[0] = c;

and you are good to go.

你很高兴。

Or of course, if this is a harcoded value, then you could do:

或者当然,如果这是一个带符号的值,那么你可以这样做:

char str[2] = "a";