2.7: Pointers and Literals
- Page ID
Pointers and String literals
String literals are arrays containing null-terminated character sequences. String literals are arrays of type character plus terminating null-character, with each of the elements being of type const char (as characters of string can’t be modified).
const char * ptr = "geek";
This declares an array with the literal representation for “geek”, and then a pointer to its first element is assigned to ptr. If we imagine that “geek” is stored at the memory locations that start at address 1800, we can represent the previous declaration as:
As pointers and arrays behave in the same way in expressions, ptr can be used to access the characters of string literal. For example:
char x = *(ptr+3);
char y = ptr;
Here, both x and y contain k stored at 1803 (1800+3).
"Pointers in C/C++ with Examples" by Abhirav Kariya, Geeks for Geeks is licensed under CC BY 4.0