Initializing Char Arrays

Hi,

I wrote a simple program that takes a users information and displays it back to the user. For my char arrays I ran a forloop and initialized them to blank spaces(" "). I know that you can also initialize to ('\n') or NULL.

I just wanted to know what is the best option to use. Iv tested my program over and over again with special characters, blank spaces ect. Its display everything the way it should. I just wanted to know what is safer to use.

Thank you,

Ammo
The safer option is to use C++ string objects.
closed account (zb0S216C)
Ammo wrote:
"For my char arrays I ran a forloop and initialized them to blank spaces(" ")."

That's assignment, my friend. Initialisation is giving a variable/object a value during a declaration context.

You can initialise an array by using a brace-enclosed initialiser-list:

1
2
char Array_[10] = {0};   // Initialises all 10 elements to null.
char Array_[10] = {'a'}; // Initialises the first element to 'a' and the rest to null. 

See here: http://www.cplusplus.com/doc/tutorial/arrays/

Wazzak
Last edited on
Framework wrote:
char Array_[10] = {'a'}; // Initialises all 10 elements to 'a'.

No this initialize the first element to 'a' and the rest of the elements to '\0'.
closed account (zb0S216C)
Ah -- I'm slipping. I've Edited my post.

Wazzak
Last edited on
Topic archived. No new replies allowed.