char arrays / C-style strings

1.

What's the difference between

 
char myString1[] = "Hello World";


and
 
char myString2[] = { "Hello World" };


2.
Are 'character arrays' and 'C-style strings' synonymous terms ?
(when talking about C++)



Last edited on
What's the difference ...

Nothing.

char myString1[] = "Hello World";

and

char myString2[] = { "Hello World" };

compile to the same thing; a char array just big enough to contain the string "Hello World", including the null terminator (i.e. 12 characters), as does (with more typing):

char myString3[] = { 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '\0' };

Are 'character arrays' and 'C-style strings' synonymous terms ?

No.

A C-style string is a series of characters (chars if it's a narrow string) with a terminating null.

A C-style string is always a character array, but an array of characters is not necessarily a C-style string. A character array does not have to have a terminating null in it.

char vowels1[] = { 'a', 'e', 'i', 'o', 'u' }; // no null!

or could have several

char vowels2[] = { 'a', '\0', 'e', '\0', 'i', '\0', 'o', '\0', 'u', '\0', '\0' };

or

char vowels2[] = "a\0e\0i\0o\0u\0";

vowels2 is effectively a series of C-style strings in the same character array,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <cstring> // for strlen()
using namespace std;

int main() {
    const char vowels2[] = { 'a', '\0', 'e', '\0', 'i', '\0', 'o', '\0', 'u', '\0', '\0' };

    const char* p = vowels2;
    while(*p != '\0') {
        cout << p << "\n";
        p += strlen(p) + 1; // skip current string inc null
    }

    return 0;
}


Andy
Last edited on
Thanks for the detailed reply.


Nothing.

char myString1[] = "Hello World";

and

char myString2[] = { "Hello World" };

compile to the same thing; a char array just big enough to contain the string "Hello World", including the null terminator (i.e. 12 characters), as does (with more typing):

char myString3[] = { 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '\0' };


Understood.


A C-style string is a series of characters (chars if it's a narrow string) with a terminating null.


Partly not understood.

'narrow string' is unfamiliar to me. Qick googling shows there are obviously narrow and wide character strings, have to read up on this.

Besides, I always took 'chars' as short form (synonymous) to 'characters'.
Obviously not as I gather from your reply ?


A C-style string is always a character array, but an array of characters is not necessarily a C-style string. A character array does not have to have a terminating null in it...


Understood.





Last edited on
narrow string = string of char (which are usually 8 bit, though this is not required by the standard. They do have to be (a) at least 8 bit, and (b) the same size as a byte is for the architecture.)

wide char = string of wchar_t which is 2-byte UTF-16 for Windows but 4 bytes UTF-32 for Linux and OS X.

https://en.wikipedia.org/wiki/Unicode
https://en.wikipedia.org/wiki/UTF-16
https://en.wikipedia.org/wiki/UTF-32

Due to confusion with wchar_t (Windows vs ROW, I presume?), C++11 introduced additional, sized character types: char16_t and char32_t.

I always took 'chars' as short form (synonymous) to 'characters'.

when you've got Unicode to deal with that breaks down.

Andy
Last edited on
Just a quick follow up on this...

Some things in above reply are over my head for now as beginner (e.g. I have now idea what ROW is), but since keywords and links are given I can check for further info and my main questions are answered I'll mark as solved.

Thanks.
Sorry -- ROW isn't techie...

Rest Of World

Andy
Haha, I did assume it would be something techie.

Thanks for clarifying.
Registered users can post here. Sign in or register to post.