14 lines
No EOL
670 B
Markdown
14 lines
No EOL
670 B
Markdown
Basics
|
|
|
|
- const char vs char
|
|
`const char *p`: This declares a pointer p that points to a constant character (immutable). It means that the data pointed to by p cannot be modified through p. However, p itself can be changed to point to a different memory location.
|
|
So `p[0]='H'` is invalid as this constant say 'hello' is read only
|
|
but if it is simply `char *p` then `p[0]='H'` is a valid operation
|
|
|
|
**Wierd Case**
|
|
if
|
|
`const char *p` is equivalent to 'hello' and we do
|
|
`char *q=p;`
|
|
and
|
|
`q[0]='H'`
|
|
then it is invalid bcz constant pointed by p is still read only and no matter if another pointer is pointing to it, that constant is still read only. |