-4

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 :

Method 1.

host = "";

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

Method 2.

host = "                   ";

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

Method 3.

host = '                   ';

=> cannot convert from 'int' to '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 :-( ?

Dominique
  • 16,450
  • 15
  • 56
  • 112
  • 1
    Have you even tried to google "*c char array initialize*"? http://stackoverflow.com/questions/18688971/c-char-array-initialization – Stefan Falk Dec 03 '14 at 11:03
  • 2
    How have you declared `host` ? – Sean Dec 03 '14 at 11:04
  • Don't you use single quotes for chars ?Also if you want to populate the array you have to do so when the variable is declared. Otherwise you have to change each individual element. –  Dec 03 '14 at 11:04
  • Show the relevant parts of your source code – Jabberwocky Dec 03 '14 at 11:10

4 Answers4

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

will fill it with zeroes.

axiac
  • 68,258
  • 9
  • 99
  • 134
2
  1. Do you know, the meaning of initialize? Usually, it's performed at defining time only.
  2. declared somewhere outside of my unit, or do you mean defined?

If host is really declared elsewhere, while defining it, you may use

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.

Check the man page here.

Dominique
  • 16,450
  • 15
  • 56
  • 112
Natasha Dutta
  • 3,242
  • 21
  • 23
1
host = "";

Will work only if the it is char* and not char[20] (if you are doing a function just make the parameter char*). The

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

It's ok but, first use

{'character', 'character','character'... 

(substitute the character with any character of ASCII for example \0).

Secondly (that is the main problem here) you have a space, where it shouldn't be (in the end, so delete this) .

Dominique
  • 16,450
  • 15
  • 56
  • 112
Rasty
  • 301
  • 1
  • 10
0

I am thinking that you have declared the host variable as an integer variable. If you have declared that as char variable,

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]=' ';

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

It is not possible because in this it will take the corresponding characters not a string.

Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31