Pointer to const char vs char array vs std :: string

Here I have two lines of code

const char * s1 = "test"; char s2 [] = "test"; 

Both lines of code have the same behavior, so I see no difference whether I should prefer s1 over s2 or vice versa. In addition to s1 and s2, there is also a way to use std::string . I think the way to use std :: string is the most elegant. Looking at another code, I often see that people either use const char * or char s [] . So my question now is when should I use const char * s1 or char s [] or std::string ? What are the differences and in what situations should I use which approach?

+8
c ++
source share
7 answers
 const char * s1 = "test"; char s2 [] = "test"; 

These two are not identical. s1 immutable: it indicates read-only memory. Modifying string literals is undefined behavior.

And yes, in C ++ you prefer std::string .

+9
source share
 POINTERS -------- char const* s1 = "test"; // pointer to string literal - do not modify! char* s1 = "test"; // pointer to string literal - do not modify! // (conversion to non-const deprecated in C++03 and // disallowed in C++11) ARRAYS ------ char s1[5] = "test"; // mutable character array copied from string literal // - do what you like with it! char s1[] = "test"; // as above, but with size deduced from initialisation CLASS-TYPE OBJECTS ------------------ std::string s1 = "test"; // C++ string object with data copied from string // literal - almost always what you *really* want 
+9
source share

The first constant, the second - no. std :: string is a type of class and implements many useful functions and methods for manipulating strings, which makes it much easier and more convenient. C-style strings with char pointers are hard to control, manipulate, and often cause errors, but don't have the overhead of std :: string. It is generally best to stick with std :: strings because they are easier to maintain.

+3
source share

The only difference between the two that you need is the following:

Which project is already in use?

+3
source share

These two do not have the same behavior. s1 is a simple pointer that is initialized to point to some (usually read-only) memory area. s2 , on the other hand, defines a local array of size 5 and populates it with a copy of this string.

Formally, you are not allowed to change s1 , that is, to do something like s1[0] = 'a' . In particular, under strange circumstances, this can lead to the fact that all other "test" in your program will become "aest" , because they all have the same memory. This is why modern compilers scream when you write

 char* s = "test"; 

On the other hand, s2 modification is allowed, as this is a local copy.

In other words, in the following example

 const char* s1 = "test"; const char* s2 = "test"; char s3[] = "test"; char s4[] = "test"; 

s1 and s2 can very accurately indicate the same address in memory, and s3 and s4 - two different copies of the same line and are in different areas of memory.

If you write C ++, use std::string if you don't need an array of characters. If you need a modifiable array of characters, use char s[] . If you only need an immutable string, use const char* .

+3
source share

which will be used depends on your requirement. The pointer offers you more flexibility. and in some cases instability. Strings are a safe option and they provide Iterator support.

0
source share

Use std::string if you don't know why do we need a char array / pointer to char.

0
source share

All Articles