How to use pointer to const in C++

1 Answer

0 votes
#include <iostream>

int main() {
    const int n = 948;

    const int *p = &n;
    //*p = 20; // error: assignment of read-only location ‘* p’

    std::cout << n << "\n";
    std::cout << *p << "\n";
}




/*
run:

948
948

*/

 



answered May 13, 2021 by avibootz

Related questions

1 answer 197 views
197 views asked Jan 1, 2021 by avibootz
9 answers 615 views
615 views asked Jul 28, 2017 by avibootz
1 answer 134 views
1 answer 138 views
1 answer 139 views
1 answer 132 views
5 answers 23 views
...