Is the type of a string, like "hello, world" a char * or const char *, as of C99? I know that in C++ it is the latter, but what about in C?
 
    
    - 714,442
- 84
- 1,110
- 1,523
- 
                    No, `std::string` is the string type in `c++`. The above are just pointer types in both languages. The "stringiness" of a buffer in `c` is decided by convention. – StoryTeller - Unslander Monica Dec 24 '15 at 16:30
- 
                    2@StoryTeller the type of a *string literal* has nothing to do with `std::string`. In C and C++ the above are `char [N]` and `char const[N]` respectively. – Nik Bougalis Dec 24 '15 at 16:31
- 
                    @NikBougalis, he presented a misconception about `c++`, be it by a poor choice of words or his own misunderstanding. My comment is not an attempt to answer his question (which I understand perfectly, thank you). – StoryTeller - Unslander Monica Dec 24 '15 at 16:34
- 
                    Why the downvote? Is it because in C++ the string type is `std::string`? – Dec 24 '15 at 16:35
- 
                    I didn't downvote, so I couldn't tell you. – StoryTeller - Unslander Monica Dec 24 '15 at 16:36
- 
                    The type is `char []` – Michi Dec 24 '15 at 17:10
4 Answers
String literals in C are not pointers, they are arrays of chars. You can tell this by looking at sizeof("hello, world"), which is 13, because null terminator is included in the size of the literal.
C99 allows string literals to be assigned to char *, which is different from C++, which requires const char *.
 
    
    - 714,442
- 84
- 1,110
- 1,523
- 
                    well to be fair, every array of `n` length of `s size objects in C is actually a ponter to `n` consecutive `s` size data in memory. But JoachimPileborgs' answer discusses this in more detail. – erik258 Dec 24 '15 at 16:38
- 
                    4@DanFarrell Although array's name is interpreted as a pointer to its initial element in many contexts, saying that an array *is* actually a pointer would not be entirely correct. Contexts where array's name is not interpreted as a pointer to array's initial element are `sizeof` and pointer arithmetics. – Sergey Kalinichenko Dec 24 '15 at 16:45
- 
                    
String literals are of type char[N] in C. For example, "abc" is an array of 4 chars (including the NUL terminator).
 
    
    - 117,907
- 20
- 175
- 238
The type of a string literal in C is char[]. This can directly be assigned to a char*. In C++, the type is const char[] as all constants are marked with const in C++.
 
    
    - 973
- 5
- 13
A character literal is always an array of read-only characters, with the array of course including the string terminator. As all arrays it of course decays to a pointer to the first element, but being read-only makes it a pointer to a const. It originated in C and was inherited by C++.
The thing is that C99 allows the weaker char * (without const) which C++ (with its stronger type system) does not allow. Some compilers may issue a warning if making a non-constant char * point to a string literal, but it's allowed. Trying to modify the string through the non-const char * of course leads to undefined behavior.
I don't have a copy of the C11 specification in front of me, but I don't think that C11 makes this stronger.
 
    
    - 400,186
- 35
- 402
- 621