简体   繁体   English

如何在字符数组中使用默认值?

[英]How to use default values in a char array?

I have the easiest question possible: how to initialise a value in C?我有一个最简单的问题:如何在 C 中初始化一个值? My variable is of the type char[20] and it is declared somewhere outside of my unit.我的变量是 char[20] 类型,它是在我的单元之外的某个地方声明的。 It is impossible to change the type.改变类型是不可能的。

Now I would like to give it a default value, let's say all empty characters (or spaces, whatever), but this is not working at all.现在我想给它一个默认值,假设所有空字符(或空格,等等),但这根本不起作用。 I have already tried :我已经尝试过:

Method 1.方法一。

host = "";

=> cannot convert from 'const char [1]' to 'char [20]' => 无法从“const char [1]”转换为“char [20]”

Method 2.方法二。

host = "                   ";

=> cannot convert from 'const char [20]' to 'char [20] => 无法从 'const char [20]' 转换为 'char [20]

Method 3.方法三。

host = '                   ';

=> cannot convert from 'int' to 'char [20] => 不能从 'int' 转换为 'char [20]

Method 4.方法四。

host = {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", };

=> syntax error : '{' => 语法错误:'{'

... ...

I am getting desperate here: how and why have the inventors of C made it so difficult to simply declare a value to a variable :-( ?我在这里感到绝望:C 的发明者如何以及为什么使简单地向变量声明值变得如此困难:-(?

memset(&host, 0, sizeof(host));

将用零填充它。

host = "";

Will work only if the it is char* and not char[20] (if you are doing a function just make the parameter char*).仅当它是 char* 而不是 char[20] 时才有效(如果您正在执行一个函数,只需将参数设置为 char*)。 The

host = {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", };

Is ok but, first use可以,但是,第一次使用

{'character', 'character','character'... {'性格','性格','性格'...

(substitute the character with any character of ASCII for example \\0). (用任何 ASCII 字符替换该字符,例如 \\0)。

and secondly (that is the main problem here) you have a , where it shoudln't be (in the end, so delete this) .其次(这是这里的主要问题)你有一个 ,它不应该在的地方(最后,所以删除它)。

  1. Do you know, the meaning of initialize ?你知道initialize的意思吗? Usually, it's performed at defining time only.通常,它仅在定义时间执行。
  2. declared somewhere outside of my unit , or do you mean defined ? declared somewhere outside of my unit ,或者你的意思是defined

If host is really declared elsewhere, while defining it, you may use如果host真的在别处declared ,在defining它时,你可以使用

char host[20] = {0};   //to fill with 0

or或者

char host[20] = { [0 ... 19] = 5 };  //to fill with 5 , supported on gcc

At any later point, if you want to re-initialize to some value, memset() is the way to go.在以后的任何时候,如果你想re-initialize为某个值, memset()是要走的路。 Check the man page here .此处查看手册页。

I am thinking that you have declared the host variable as an integer variable.我认为您已将宿主变量声明为整数变量。 If you have declared that as char variable,如果您已将其声明为 char 变量,

host = "";
host = "     ";

These two are possible.这两个是可能的。 Because it will take the characters for corresponding position.因为它会取对应位置的字符。

host = ' '; It is not possible because in single quotes we have to give the character only.这是不可能的,因为在单引号中我们只需要给出字符。 In this you have to mention the array position otherwise it will throw the error.在此您必须提及数组位置,否则会引发错误。 host[0]=' ';主机[0]='';

host = {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", };

It is not possible because in this it will take the corresponding characters not a string.这是不可能的,因为在这里它将采用相应的字符而不是字符串。

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

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