In C++ whats the difference between
char const *ptr=&ch;
and
const char *ptr=&ch;
Pointers must be declared before they can be used, just like a normal variable. The syntax of declaring a pointer is to place a * in front of the name. A pointer is associated with a type (such as int and double ) too.
A pointer is a variable that stores the address of another variable. Unlike other variables that hold values of a certain type, pointer holds the address of a variable. For example, an integer variable holds (or you can say stores) an integer value, however an integer pointer holds the address of a integer variable.
You need to initialize a pointer by assigning it a valid address. This is normally done via the address-of operator (&). The address-of operator (&) operates on a variable, and returns the address of the variable. For example, if number is an int variable, &number returns the address of the variable number.
They are the same, i.e. pointer to const char
.
However char * const ptr
is different, being a const pointer to (non-const) char
.
And just to complete the set, const char * const ptr
is a const pointer to const char
.
No difference in C++.
It is important const
is before *
or after *
.
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