char *p = "string"; //creates pointer to constant string
char p[] = "string"; //just an array with "string"
I'm just a bit confused about why does it in the first example create a pointer to a constant string? Shouldn't it be just a pointer refering to a place in memory with "string"?
We declare a pointer to constant. First, we assign the address of variable 'a' to the pointer 'ptr'. Then, we assign the address of variable 'b' to the pointer 'ptr'. Lastly, we try to print the value of 'ptr'.
A pointer to a const value (sometimes called a pointer to const for short) is a (non-const) pointer that points to a constant value. In the above example, ptr points to a const int . Because the data type being pointed to is const, the value being pointed to can't be changed. We can also make a pointer itself constant.
String Literals. A String Literal, also known as a string constant or constant string, is a string of characters enclosed in double quotes, such as "To err is human - To really foul things up requires a computer." String literals are stored in C as an array of chars, terminted by a null byte.
A constant pointer is declared as : int *const ptr ( the location of 'const' make the pointer 'ptr' as constant pointer) 2) Pointer to Constant : These type of pointers are the one which cannot change the value they are pointing to. This means they cannot change the value of the variable whose address they are holding.
In the first case, "string"
may be stored in a read-only area of the process, so attempting to modify the memory pointed to by p
would cause undefined behaviour.
In the second case, actual memory is allocated and initialised on the stack at runtime (or in an appropriate section of the process at program startup if it's not a local variable), so modifying the memory is OK.
The first case can be illustrated like this:
+---+ +---+---+---+---+---+---+---+
| | ----> | s | t | r | i | n | g | \0|
+---+ +---+---+---+---+---+---+---+
p
Whereas the second case is:
+---+---+---+---+---+---+---+
| s | t | r | i | n | g | \0|
+---+---+---+---+---+---+---+
p
The two declarations have a significant difference, although you may have heard that they are the same from low-quality C programming books.
The first is a pointer of type char *
, whereas the second is an array of type char []
. Array identifiers decay to a pointer in some contexts, but that doesn't make them pointers.
A pointer is merely an address and an array is "the whole thing" - in the first case sizeof(p)
yields the size of a pointer (usually 4
or 8
depending on the target machine), and in the second case it yields 7 * sizeof(char)
, the length of the actual string.
It is unfortunately legal in C (and in C++03, for compatibility). But any attempt to modify the string literal via the pointer will result in Undefined behavior. So, better always assign the string literal to a const char*
const char * cp = "Hello"; //OK
char* p = "Hello"; //OK in C and C++03 (unfortunately), Illegal in C++11
cp[0] = 'Y'; //Compile-time error, good
p[0] = 'Y'; //no compiler error, undefined behavior
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