Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ How do I set an entire character array to blank spaces?

Tags:

c++

Say I have the following declared variable:

char mychararray[35];

and I want to set every character in the array to a blank space... How can I do that?

My instructor told me all I had to do was put

mychararray = "";

but that didn't work at all...

Am I using an outdated version of Visual Studio (2012), or is this just a bad initialization? If it is the latter, please explain how to make all the characters a blank space.

Thanks in advance!

like image 443
j0hnnyb0y Avatar asked Feb 08 '15 18:02

j0hnnyb0y


1 Answers

You can initialize it the way your instructor suggested as you declare the array:

char mychararray[35] = "";

It will set the array to an empty string.

If you want to make it an empty string later, then you can just do

mychararray[0] = '\0';

If you want to make it an array consisting of 34 spaces (35th character being null terminator), then

memset(mychararray, ' ', 34);
mychararray[34] = '\0';
like image 189
Ishamael Avatar answered Nov 09 '22 14:11

Ishamael