J Shankar wrote:Please help me to understand "how the pointer is represented for Char and String" in the above scenario.
A string literal produces a "null-terminated string" in the generated code. This is a series of characters followed by a zero byte. So,
"I am a string" takes up 14 bytes: one for each of the 13 characters in the string, plus a final zero byte.
A pointer holds an address. On 32-bit architectures this is usually a single 32-bit value which the processor can use to identify the location of some data. If you say
Then
ptr is declared as a pointer to
char. In this case, the pointer holds the address of the first character in the string,
I. Since the subsequent characters in the string are stored at adjacent locations, your code can find the rest of the string from this pointer to the first character, checking each character in turn until it finds the zero byte at the end of the string --- this is why the string is null-terminated.
The pointer-dereference operator
* retrieves the value stored at the pointed-to location. Since
ptr is a pointer-to-
char, the value stored is a character, in this case the [b]I[/bb] that is the first character in the string.
When you print things with
printf,
%c says to print a character, and
%s to print a null-terminated string identified by a pointer to its first character, so you get the
I I am a string output you describe.