C++ | const keyword | Question 1
Predict the output of following program
#include <iostream> using namespace std; int main() { const char* p = "12345"; const char **q = &p; *q = "abcde"; const char *s = ++p; p = "XYZWVU"; cout << *++s; return 0; } |
chevron_right
filter_none
(A) Compiler Error
(B) c
(C) b
(D) Garbage Value
Answer: (B)
Explanation: Output is ‘c’
const char* p = “12345″ declares a pointer to a constant. So we can’t assign something else to *p, but we can assign new value to p.
const char **q = &p; declares a pointer to a pointer. We can’t assign something else to **q, but we can assign new values to q and *q.
*q = “abcde”; changes p to point to “abcde”
const char *s = ++p; assigns address of literal ”bcde” to s. Again *s can’t be assigned a new value, but s can be changed.
The statement printf(“%cn”, *++s) changes s to “cde” and first character at s is printed.
Quiz of this Question
Recommended Posts:
- C++ | const keyword | Question 5
- C++ | const keyword | Question 3
- C++ | const keyword | Question 5
- C++ | const keyword | Question 2
- Function overloading and const keyword
- Difference between const int*, const int * const, and int const *
- Difference between const char *p, char * const p and const char * const p
- C++ | Static Keyword | Question 2
- C++ | Static Keyword | Question 3
- C++ | friend keyword | Question 1
- C++ | Static Keyword | Question 1
- C++ | Static Keyword | Question 4
- C++ | friend keyword | Question 2
- C++ | Static Keyword | Question 5
- C++ | Static Keyword | Question 6



