C语言 一个如何表示空字符?

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

How does one represent the empty char?

c

提问by ardent

I'm currently writing a little program but I keep getting this error when compiling

我目前正在编写一个小程序,但在编译时我不断收到此错误

error: empty character constant

错误:空字符常量

I realize it's because I'm trying to replace a valid char with empty space c[i]=''but I have not been able to find another way to represent it.

我意识到这是因为我试图用空白替换一个有效的字符,c[i]=''但我无法找到另一种表示它的方法。

回答by ardent

You can use c[i]= '\0'or simply c[i] = (char) 0.

您可以使用c[i]= '\0'或简单地使用c[i] = (char) 0.

The null/empty char is simply a value of zero, but can also be represented as a character with an escaped zero.

空/空字符只是零值,但也可以表示为带有转义零的字符。

回答by occulus

You can't store "no character" in a character - it doesn't make sense.

你不能在一个字符中存储“无字符”——这是没有意义的。

As an alternative you could store a character that has a special meaning to you - e.g. null char '\0'- and treat this specially.

作为替代方案,您可以存储一个对您具有特殊含义的字符'\0'-例如空字符- 并对其进行特殊处理。

回答by Claudiu

The empty space char would be ' '. If you're looking for null that would be '\0'.

空白字符将是' '. 如果您正在寻找 null 那将是'\0'.

回答by Grijesh Chauhan

Yes, c[i]=''is not a valid code. We parenthesis character constant between '', e.g. c[i] = 'A';char A. but you don't write any char in between ''.

是的,c[i]=''不是有效代码。我们在 之间加上括号字符常量'',例如c[i] = 'A';char A。但你不要在两者之间写任何字符''

Empty space is nothing but suppose if you wants to assigned space then do:

空白空间不过是假设如果您想分配空间,请执行以下操作:

c[i] = ' ';
//      ^  space 

if wants to assigned nul charthen do:

如果要分配,nul char请执行以下操作:

c[i] = '
char c[10] = {'a', '2', 'c', '
c[1] = ' ';
'};
'; // ^ null symbol

Example: Suppose if c[]a string (nul \0terminated char array) if you having a string. for example:

示例:如果您有一个字符串,假设 c[]一个字符串(以 nul\0结尾的字符数组)。例如:

printf("\n c: %s", c);

And you replace second char with space:

然后用空格替换第二个字符

  c:  a  c
//      ^ space printed 

and if you print it using printf as follows:

如果您使用 printf 打印它,如下所示:

c[1] = '
  c:  a
';

then output would be:

那么输出将是:

char** c = new char*[N];
c[0] = NULL; // no character
*c[1] = ' '; // ordinary character
*c[2] = 'a'; // ordinary character
*c[3] = '
char* c = new char[N];
c[0] = '
char* my_variable = "";
'; // no character c[1] = ' '; // ordinary character c[2] = 'a'; // ordinary character
' // zero-code character

And you replace second char with '\0':

然后用 '\0' 替换第二个字符

char my_variable = '
#include <string.h>
char* my_variable = strdup("");
';

then output would be:

那么输出将是:

char test_src[] = "fuu
{'f', 'u', 'u', '##代码##', 'f', 'o', 'o', '##代码##'}
foo";

because string terminated with \0.

因为字符串以\0.

回答by verbose

There is no such thing as the "empty character" ''.

没有“空字符”这样的东西''

If you need a space character, that can be represented as a space: c[i] = ' 'or as its ASCII octal equivalent: c[i] = '\040'. If you need a NUL character that's c[i] = '\0'.

如果您需要一个空格字符,可以将其表示为空格:c[i] = ' '或其 ASCII 八进制等效项:c[i] = '\040'。如果您需要一个 NUL 字符,那就是c[i] = '\0'.

回答by BartoszKP

To represent the fact that the value is not present you have two choices:

要表示该值不存在的事实,您有两种选择:

1) If the whole charrange is meaningful and you cannot reserve any value, then use char*instead of char:

1) 如果整个char范围都有意义并且您不能保留任何值,则使用char*代替char

##代码##

Then you'll have c[i] == NULLfor when character is not present and otherwise *c[i]for ordinary characters.

那么c[i] == NULL当字符不存在时您将拥有,否则*c[i]对于普通字符。

2) If you don't need some values representable in charthen reserve one for indicating that value is not present, for example the '\0'character.

2)如果您不需要某些可表示的值,char则保留一个用于指示该值不存在的值,例如'\0'字符。

##代码##

Then you'll have c[i] == '\0'for when character is not present and ordinary characters otherwise.

然后您将拥有c[i] == '\0'for 当字符不存在而普通字符不存在时。

回答by Nick Cuevas

There are two ways to do the same instruction, that is, an empty string. The first way is to allocate an empty string on static memory:

有两种方法可以做相同的指令,即空字符串。第一种方法是在静态内存上分配一个空字符串:

##代码##

or, if you want to be explicit:

或者,如果你想明确:

##代码##

The way posted above is only for a character. And, the second way:

上面发布的方式仅适用于一个角色。而且,第二种方式:

##代码##

Don't forget to use free()with this one because strdup()use mallocinside.

不要忘记将free()与这个一起使用,因为strdup()在内部使用malloc

回答by Kyle s

It might be useful to assign a null in a string rather than explicitly making some index the null char '\0'. I've used this for testing functions that handle strings ensuring they stay within their appropriate bounds.

在字符串中分配一个 null 而不是显式地将某个索引设为 null char 可能很有用'\0'。我用它来测试处理字符串的函数,确保它们保持在适当的范围内。

With:

和:

##代码##

This creates an array of size 8 with values:

这将创建一个大小为 8 的数组,其中包含以下值:

##代码##