Created
August 12, 2019 12:42
-
-
Save dprilutskii/3d0bec1c2cbf19bbe23789c6a60a7ce0 to your computer and use it in GitHub Desktop.
Const and pointer in C
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> | |
typedef int type_t; | |
int main(void) { | |
printf("Сhanges after initialization\n"); | |
printf("\ttest 1\n"); | |
const type_t value1 = 7; | |
const type_t value2 = 10; | |
// Указатель на константное значение | |
const type_t *ptr = &value1; | |
// можно изменить сам указатель, но нельзя изменить значение по указателю | |
ptr = &value2; | |
// запрещено присваивание вида " *ptr = ... " | |
printf("Prohibition on change after initialization\n"); | |
printf("\ttest 2\n"); | |
type_t ** const a1 = (type_t ** const)5000; // константный указатель на указатель | |
// запрещено присваивание вида " a = ... " | |
printf("\ttest 3\n"); | |
type_t * const *a2 = (type_t * const *)5000; // указатель на константный указатель | |
// запрещено присваивание вида " *a = ... " | |
// a2 = (T * const *)5; | |
printf("\ttest 4\n"); | |
type_t const **a3 = (type_t const **)5000; // указатель на указатель на константу | |
// запрещено присваивание вида " ** a = ... " | |
// a3 = (const T **)1000; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment