The difference between char * const and const char *
cconstantspointers
What's the difference between:
char * const
and
const char *
Best Answer
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).
There is also a
const char * const
which is a constant pointer to a constant char (so nothing about it can be changed).
Note:
The following two forms are equivalent:
const char *
and
char const *
The exact reason for this is described in the C++ standard, but it's important to note and avoid the confusion. I know several coding standards that prefer:
char const
over
const char
(with or without pointer) so that the placement of the const element is the same as with a pointer const.
In practice, the difference is in the location where the preprocessor searches for the included file.
For #include <filename> the preprocessor searches in an implementation dependent manner, normally in search directories pre-designated by the compiler/IDE. This method is normally used to include standard library header files.
For #include "filename" the preprocessor searches first in the same directory as the file containing the directive, and then follows the search path used for the #include <filename> form. This method is normally used to include programmer-defined header files.
++i will increment the value of i, and then return the incremented value.
i = 1;
j = ++i;
(i is 2, j is 2)
i++ will increment the value of i, but return the original value that i held before being incremented.
i = 1;
j = i++;
(i is 2, j is 1)
For a for loop, either works. ++i seems more common, perhaps because that is what is used in K&R.
In any case, follow the guideline "prefer ++i over i++" and you won't go wrong.
There's a couple of comments regarding the efficiency of ++i and i++. In any non-student-project compiler, there will be no performance difference. You can verify this by looking at the generated code, which will be identical.
As @OnFreund notes, it's different for a C++ object, since operator++() is a function and the compiler can't know to optimize away the creation of a temporary object to hold the intermediate value.
Best Answer
The difference is that
const char *
is a pointer to aconst char
, whilechar * const
is a constant pointer to achar
.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).
There is also a
which is a constant pointer to a constant char (so nothing about it can be changed).
Note:
The following two forms are equivalent:
and
The exact reason for this is described in the C++ standard, but it's important to note and avoid the confusion. I know several coding standards that prefer:
over
(with or without pointer) so that the placement of the
const
element is the same as with a pointerconst
.