Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C++ 2d char array initialization

I am wondering what would be the equivalent of the following initialization.

char array[3][32] = { "string0", "string1" , "string2"};

The above code works but it has the risk of variable being initialized more than once.

I tried this but it only got the last number instead of the whole string.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

char array[3][32];
*array[0] = 'string0';
*array[1] = 'string1';
*array[2] = 'string2';

Thank you in advance for the help!

>Solution :

You should use strcpy() to copy C-style strings.

Also '' is for character constants. You should "" for string literals.

char array[3][32];
strcpy(array[0], "string0");
strcpy(array[1], "string1");
strcpy(array[2], "string2");
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading