From d8d8f1c987f118843d44ea27ac516f8fe866ded4 Mon Sep 17 00:00:00 2001 From: Oskar Date: Tue, 17 Sep 2024 15:02:56 +0200 Subject: more --- const-pointer-tests.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 const-pointer-tests.cpp (limited to 'const-pointer-tests.cpp') diff --git a/const-pointer-tests.cpp b/const-pointer-tests.cpp new file mode 100644 index 0000000..dceda16 --- /dev/null +++ b/const-pointer-tests.cpp @@ -0,0 +1,24 @@ +#include + +/* + * + * Trying to make sense of const + * + * + */ + +int main () { + int a = 0; + int b = 0; + int c = 0; + int const* pointer1 = &a; // Cannot change underlying object, can point to other object (Please for the love of god dont use this version its so confusing, use pointer3 version instead because its the same thing and much more clear + int *const pointer2 = &b; // Can change underlying object, can not point to new object + const int* pointer3 = &c; // Cannot change underlying object, can point to other object + std::cout << pointer1 << "\n" + << pointer2 << "\n" + << pointer3 << std::endl; + + const int *const pointer4 = &a; // Cannot change underlying object, cannot point to another object + std::cout << pointer4 << std::endl; + return 0; +} -- cgit v1.2.3