Skip to content

Instantly share code, notes, and snippets.

@h1mesuke
Created July 27, 2010 13:39
Show Gist options
  • Save h1mesuke/492231 to your computer and use it in GitHub Desktop.
Save h1mesuke/492231 to your computer and use it in GitHub Desktop.
C - ポインタ変数に対するconst宣言
#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