Consider the following C code
char str[] = "iCDuLNb";
char *p = str + 1;
printf("%s", p);if the base address for str is 0x000081e0, what will be printed when the printf statement is executed?
Output: "CDuLNb"
Additionally, what is the value of p in hexadecimal?
p = 0x000081e1
Answer
When working with strings in C, remember that strings are null-terminated. This means that the end of a string is marked by the null character ('\0'), which tells functions like printf where to stop printing.
In the given example, the pointer p is initialized as str + 1, meaning it points to the character at position 1 in the string iCDuLNb. The printf("%s", p); statement prints the substring starting from that character until the null character is encountered, giving us the Output seen above.
Meanwhile, to find the address of p, we can calculate it as follows:
- The base address of
stris0x000081e0. - The offset to the character at index
1is1 * 1(since each character is 1 byte). - Therefore, the address of
pis0x000081e0 + 1.