Created
July 27, 2010 13:39
-
-
Save h1mesuke/492231 to your computer and use it in GitHub Desktop.
C - ポインタ変数に対するconst宣言
This file contains 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 argc, const char *argv[]) | |
{ | |
int i = 10; | |
int j = 20; | |
const int *p = &i; | |
int * const q = &i; | |
const int * const r = &i; | |
/**** const type *var ****/ | |
puts(""); | |
printf("*p = %d\n", *p); | |
printf(" p = %p\n", p); | |
/* error */ | |
/* *p = 100; */ | |
/* allowed */ | |
p = &j; | |
puts(""); | |
printf("*p = %d\n", *p); | |
printf(" p = %p\n", p); | |
/* error */ | |
/* *p = 100; */ | |
/**** type * const var ****/ | |
puts(""); | |
printf("*q = %d\n", *q); | |
printf(" q = %p\n", q); | |
/* error */ | |
/* q = &j; */ | |
/* allowed */ | |
*q = 100; | |
puts(""); | |
printf("*q = %d\n", *q); | |
printf(" q = %p\n", q); | |
/**** const type * const var ****/ | |
puts(""); | |
printf("*r = %d\n", *r); | |
printf(" r = %p\n", r); | |
/* error */ | |
/* *r = 100; */ | |
/* error */ | |
/* r = &j; */ | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment