5

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!

5
  • possible duplicate of initialize array to 0 in C Commented Feb 8, 2015 at 18:51
  • 2
    Assigning on one line char myarray[35] = ""; does work. All characters will be '\0'. Commented Feb 8, 2015 at 18:54
  • @zenith Not really. It doesn't "set every character in the array to a blank space". Commented Feb 8, 2015 at 18:58
  • @juanchopanza I know. I was just pointing out the OP's = "" wasn't just some bad initialization. Commented Feb 8, 2015 at 19:00
  • Please clarify what a "blank space" is. Do you want every character in the array to be a space character ' ' (with no terminating '\0'), or do you want them all to be '\0', or something else? Commented Jun 19, 2017 at 17:54

5 Answers 5

10

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';
Sign up to request clarification or add additional context in comments.

8 Comments

Ok, so I'm guessing the first option would be closest to what my instructor was trying to get me to do, correct?
@tomkelley13 No, the first option does not set the entire array to blank spaces. It sets the first element to the null terminator. All other elements are left uninitialized.
@tomkelley13, it does the same thing as if you originally wrote char mychararray[35] = ""; I updated my answer.
I used the second option, that seems to be working for me... thank you.
@juanchopanza: Are you sure? [dcl.init.string]/3 says If there are fewer initializers than there are array elements, each element not explicitly initialized shall be zero-initialized.
|
5

That is not initialization, it's assignment. For initializing an array when declaring it, you can write:

char mychararray[35] = "";

If you already have an array and want to set it to zero, you can use std::fill:

#include <algorithm>
#include <iterator>

int main() 
{
    char c[35] = "Hello, world!";
    std::fill(std::begin(c), std::end(c), '\0');
    // c will contain only zeros now.
}

However, C-style arrays are not very popular in modern C++. Prefer using std::string (which you can clear using the clear() member function), or std::array<char, N> if you need the size to be known at compile-time.

Comments

4

std::memset is your friend.

memset(mychararray, ' ', 34);
mychararray[34]='\0';

If you want to initialize the array with all elements being '\0', you have a neater way through aggregate initialization:

char mychararray[35] = {};

3 Comments

overwrites memory by 1 byte. (mychararray[35] = '\0')
The function std::fill is safer, the memset function should only be used with bytes.
@ThomasMatthews Agreed. This indeed was a char array specific answer. Since the byte being specified is a char literal, overflow isn't a worry.
3

Although you could use std::fill_n(array, 35, ' '), you're probably better of using an std::string, the preferred string class in C++. Don't have to care about zero-termination or anything.

std::string blanks(35, ' ');

Comments

0

C++ doesn't initialie char to ' ' (which has the ascii value 0x20, btw); in fact, array initialization without specifying what to use for initialization doesn't necessarily take place (arrays in local scope are left uninitialized).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.