如何在char数组中使用默认值? [英] How to use default values in a char array?

查看:99
本文介绍了如何在char数组中使用默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个最简单的问题:如何在C中初始化一个值?我的变量的类型为char [20],并且在我的单元之外的地方声明了它.无法更改类型.

I have the easiest question possible: how to initialise a value in C? My variable is of the type char[20] and it is declared somewhere outside of my unit. 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 :

方法1.

host = "";

=>无法从'const char [1]'转换为'char [20]'

=> cannot convert from 'const char [1]' to 'char [20]'

方法2.

host = "                   ";

=>无法从'const char [20]'转换为'char [20]

=> cannot convert from 'const char [20]' to 'char [20]

方法3.

host = '                   ';

=>无法从'int'转换为'char [20]

=> cannot convert from 'int' to 'char [20]

方法4.

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

=>语法错误:'{'

=> syntax error : '{'

...

我在这里感到绝望:C的发明者如何以及为什么使简单地为变量声明值如此困难:-(?

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 :-( ?

推荐答案

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

将其填充为零.

这篇关于如何在char数组中使用默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆