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 answ
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.
I was helped a lot by Ulrich Drepper's blog-entries a couple of years ago:
so close but no cigar and more array fun
The gist of the blog is that const char[]
should be preferred but only as global or static variable.
Using a pointer const char*
has as disadvantages:
If you use an array, then the data is initialized at runtime. If you use the pointer, the run-time overhead is (probably) less because only the pointer needs to be initialized. (If the data is smaller than the size of a pointer, then the run-time initialization of the data is less than the initialization of the pointer.) So, if you have enough data that it matters and you care about the run-time cost of the initialization, you should use a pointer. You should almost never care about those details.
Just a note:
I'd make it static const char sz[] = "hello";
. Declaring as such has the nice advantage of making changes to that constant string crash the program by writing to read-only memory. Without static
, casting away constness and then changing the content may go unnoticed.
Also, the static
lets the array simply lie in the constant data section instead of being created on the stack and copied from the constant data section each time the function is called.
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.
In general to answer these types of questions, use the one that's most explicit.
In this case, const char[]
wins because it contains more detailed information about the data within -- namely, the size of the buffer.