How to declare read only pointer without the option to modify the content of the pointer in C++

1 Answer

0 votes
#include <iostream>

int main() {
    int a = 123;
    const int* const p = &a; 
    std::cout << *p << ' ' << a << '\n';

    //*p = 99; // error: assignment of read-only location ‘*(const int*)p’

    int b = 905;
    // p = &b; //  error: assignment of read-only variable ‘p’
}




/*
run:

123 123

*/

 



answered Feb 25, 2022 by avibootz

Related questions

1 answer 225 views
1 answer 130 views
1 answer 184 views
184 views asked Feb 25, 2022 by avibootz
2 answers 347 views
1 answer 175 views
175 views asked Jul 8, 2020 by avibootz
...