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!

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
j0hnnyb0y
  • 67
  • 1
  • 1
  • 7
  • possible duplicate of [initialize array to 0 in C](http://stackoverflow.com/questions/2589749/initialize-array-to-0-in-c) – JuniorCompressor Feb 08 '15 at 18:51
  • 2
    Assigning on one line `char myarray[35] = "";` does work. All characters will be `'\0'`. – Emil Laine Feb 08 '15 at 18:54
  • @zenith Not really. It doesn't "set every character in the array to a blank space". – juanchopanza Feb 08 '15 at 18:58
  • @juanchopanza I know. I was just pointing out the OP's `= ""` wasn't just some bad initialization. – Emil Laine Feb 08 '15 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? – Technophile Jun 19 '17 at 17:54

5 Answers5

9

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';
Ishamael
  • 12,583
  • 4
  • 34
  • 52
  • Ok, so I'm guessing the first option would be closest to what my instructor was trying to get me to do, correct? – j0hnnyb0y Feb 08 '15 at 18:55
  • @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. – juanchopanza Feb 08 '15 at 18:56
  • @tomkelley13, it does the same thing as if you originally wrote `char mychararray[35] = "";` I updated my answer. – Ishamael Feb 08 '15 at 19:10
  • I used the second option, that seems to be working for me... thank you. – j0hnnyb0y Feb 08 '15 at 19:11
  • @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.` – Andy Prowl Feb 08 '15 at 19:17
  • @AndyProwl My comment predates the edit. The first option became the second one. – juanchopanza Feb 08 '15 at 19:21
  • @juanchopanza: Ah, I see. I take it back :) – Andy Prowl Feb 08 '15 at 19:22
  • This helped me get a string setup properly for output to an LCD on Arduino (Nano) platform. I used memset() and set the last byte to \0. Thanks – raddevus May 24 '21 at 20:08
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.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
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] = {};
Pradhan
  • 16,391
  • 3
  • 44
  • 59
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, ' ');
xtofl
  • 40,723
  • 12
  • 105
  • 192
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).

timrau
  • 22,578
  • 4
  • 51
  • 64
Marcus Müller
  • 34,677
  • 4
  • 53
  • 94