Created
August 21, 2018 14:06
-
-
Save lovelock/b36bd09166c283bbd93b4c61a82658ca to your computer and use it in GitHub Desktop.
describe pointers and const's
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main() | |
{ | |
/* | |
* int const p = 20; // p is a const int | |
* printf("p = %d\n", p); | |
*/ | |
/* | |
* const int pi = 20; | |
* printf("pi = %d\n", pi); | |
*/ | |
/* | |
* const int *p; // p is a pointer to int const, that is to say, *p is read-only | |
* int const d = 300; | |
* p = &d; | |
* printf("int const @p = %d\n", *p); | |
* int const f = 500; | |
* // *p = f; // read-only variable is not assignable | |
*/ | |
/* | |
* int const *p; // p is a pointer to const int, that is to say, *p is read-only | |
* const int d = 300; | |
* p = &d; | |
* printf("int const @p = %d\n", *p); | |
* int const f = 500; | |
* // *p = f; // read-only variable is not assignable | |
*/ | |
/* | |
* int d = 300; | |
* int *const p = &d; // p is a const pointer to int, that is to say, p is read-only but the value p points to is mutable | |
* printf("int @p = %d\n", *p); | |
* d = 500; | |
* printf("int @p = %d\n", *p); | |
* *p = 800; | |
* printf("int @p = %d\n", *p); | |
* printf("int d = %d\n", d); | |
*/ | |
/** | |
* There is no const *int p; | |
*/ | |
/* | |
* int const d = 300; | |
* const int *const p = &d; // p is a const pointer to int const, that is to say, p and *p are both read-only | |
* // p = &d; // cannot assign to variable 'p' with const-qualified type 'const int *const' | |
* printf("int const @ const pointer p is %d\n", *p); | |
*/ | |
/* | |
* const int d = 300; | |
* int const *const p = &d; // p is a const pointer to const int | |
* // p = &d; // cannot assign to variable 'p' with const-qualified type 'const int *const' | |
* printf("const int @ const pointer p is %d\n", *p); | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment