9

I want a 20 character NULL('\0') terminating string filled with white spaces.

Currently I am doing it in following way

char foo[20];  
for (i = 0; i < num_space_req; i++)        //num_space_req < 20  
{  
    foo[i] = ' ';  
}

foo[num_space_req] = '\0';

Is there a better way for above?

3
  • 8
    std::fill (foo, foo + num_space_req, ' '); Commented Apr 30, 2012 at 13:40
  • 4
    Please try to avoid tagging questions with both C and C++ unless you really need a cross-language solution. They are vastly different languages once you remove all the legacy C stuff that C++ coders shouldn't be using anyway :-) Commented Apr 30, 2012 at 13:42
  • You've gotten a lot of good answers. You might want to accept one of them. Commented Apr 30, 2012 at 17:15

8 Answers 8

18

You can use the following to initialize the array to spaces:

memset(foo, ' ', num_space_req);
foo[num_space_req] = '\0';
Sign up to request clarification or add additional context in comments.

3 Comments

I didn't downvote, but using memset wouldn't be my first choice.
@bames53: Given that this question is tagged (amongst other things) as C, what would you choose?
Tagging questions with both C and C++ is usually a result of ignorance on the part of the asker, so I always take it to mean a C++ solution is acceptable. Otherwise they'd just tag it as C.
7
std::string foo(num_space_req,' ');

Comments

7

Since the question has a C++ tag, the idiomatic way would be:

std::fill(foo, foo + num_space_req, ' ');
// or
std::fill_n(foo, num_space_req, ' ');

Note that it doesn't work in C.

1 Comment

good call bringing in the c++ idiom, but isn't this answer incomplete in that it doesn't place a null terminator?
2

You may use memset for that kind of thing.

memset (foo, ' ', num_space_req)

http://www.cplusplus.com/reference/clibrary/cstring/memset/

Comments

2

As @OliCharlesworth said, the best way is to use memset:

char bla[20];

memset(bla, ' ', sizeof bla - 1);
bla[sizeof bla - 1] = '\0';

Note that in GNU C, you can also use the following extension (range designated initializers):

char bla[20] = {[0 ... 18] = ' ', [19] = '\0'};

Comments

2

If you want the array initialized at compile time, you can modify your char foo[20]; declaration as follows:

char foo[20] = {0x20};

If you need to initialize the array to spaces at run time, then you can use the following:

memset(foo, ' ', sizeof(foo) -1);
foo[20] = '\0';

Comments

0

memset MIGHT be better optimised than your for loop, it might be exactly the same. pick one of:

memset( foo, ' ', sizeof(foo) -1 );
memset( foo, ' ', 19 );
memset( foo, ' ', 19 * sizeof(char) );

Comments

0
#include <cstring>
void makespace(char *foo, size_t size) {
  memset((void*)&foo, ' ', size - 1);
  foo[size] = 0;
}
// ...
makespace(foo, 20);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.