I know they are different, I know how they are different and I read all questions I could find regarding char*
vs char[]
But all those answers never tell when they should be used.
So my question is:
When do you use
const char *text = "text";
and when do you use
const char text[] = "text";
Is there any guideline or rule?
As an example, which one is better:
void withPointer() { const char *sz = "hello"; std::cout << sz << std::endl; } void withArray() { const char sz[] = "hello"; std::cout << sz << std::endl; }
(I know std::string
is also an option but I specifically want to know about char
pointer/array)
The difference is that const char * is a pointer to a const char , while char * const is a constant pointer to a char . The first, the value being pointed to can't be changed but the pointer can be. The second, the value being pointed at can change but the pointer can't (similar to a reference).
For the array, the total string is stored in the stack section, but for the pointer, the pointer variable is stored into stack section, and content is stored at code section. And the most important difference is that, we cannot edit the pointer type string.
const char* const says that the pointer can point to a constant char and value of int pointed by this pointer cannot be changed. And we cannot change the value of pointer as well it is now constant and it cannot point to another constant char.
If you don't have the choice, using const char* gives a guarantee to the user that you won't change his data especially if it was a string literal where modifying one is undefined behavior. Show activity on this post. By using const you're promising your user that you won't change the string being passed in.
Both are distinctly different, For a start:
Read on for more detailed explanation:
char text[] = "text";
Creates an array that is large enough to hold the string literal "text", including its NULL
terminator. The array text
is initialized with the string literal "text".The array can be modified at a later time. Also, the array's size is known even at compile time, so sizeof
operator can be used to determine its size.
char *text = "text";
Creates a pointer to point to a string literal "text". This is faster than the array version, but string pointed by the pointer should not be changed, because it is located in an read only implementation defined memory. Modifying such an string literal results in Undefined Behavior.
In fact C++03 deprecates use of string literal without the const
keyword. So the declaration should be:
const char*text = "text";
Also,you need to use the strlen()
function, and not sizeof
to find size of the string since the sizeof
operator will just give you the size of the pointer variable.
Depends on the Usage.
EDIT: It was just brought to my notice(in comments) that the OP seeks difference between:
const char text[]
and const char* text
Well the above differing points still apply except the one regarding modifying the string literal. With the const
qualifier the array test
is now an array containing elements of the type const char
which implies they cannot be modified.
Given that, I would choose the array version over the pointer version because the pointer can be(by mistake)easily reseated to another pointer and the string could be modified through that another pointer resulting in an UB.
Probably the biggest difference is that you cannot use the sizeof
operator with the pointer to get the size of the buffer begin pointed to, where-as with the const char[]
version you can use sizeof
on the array variable to get the memory footprint size of the array in bytes. So it really depends on what you're wanting to-do with the pointer or buffer, and how you want to use it.
For instance, doing:
void withPointer() { const char *sz = "hello"; std::cout << sizeof(sz) << std::endl; } void withArray() { const char sz[] = "hello"; std::cout << sizeof(sz) << std::endl; }
will give you very different answers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With