What will be the output of the following C++ code using a character pointer?

#include <iostream>
using namespace std;
int main() {
    char s[] = "HELLO";
    char *p = s;
    cout << *(p + 2);
    return 0;
}
                        

Options

  • A. H
  • B. E
  • C. L
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

C. L

Detailed Explanation

Correct Answer: Option C — L

Explanation: p points to the first character of s. The expression p + 2 points to the third character, and *(p + 2) dereferences that pointer. The third character of HELLO is L.