Download : sample-001.c ( SJIS 版 )
/* * 2014/01/10 sample-001.c */ /* * */ #include <stdio.h> /* * */ int main ( void ) { int a = 111; /* 並んだ変数は、恐らく、連続したメモリにある(保証はない) */ int b = 222; /* a, b, c の順か c, b, a の順かは解らない */ int c = 999; /* 並んでいれば b は a と c の間にある */ /* それならば、「&a + ((&c-&a)/2) 」は「&b」に一致する筈 */ /* * 以下の結果は、人によって異なるし、場合によれば、毎回異なるかもしれない */ printf ( "&a = %x\n", &a ); printf ( "&b = %x\n", &b ); printf ( "&c = %x\n", &c ); /* * 次の結果は、(a,b,c が並んでいれば..) 絶対値は 2 になる */ printf ( "&c - &a = %d\n", &c - &a ); /* * 次のアドレス計算の結果は、&b と同じになるはず.. */ printf ( "&a + (&c-&a)/2 = %x ( &b = %x )\n", &a + (&c-&a)/2, &b ); /* * 変数 a と変数 c の間の変数 (b) の値を出す * 「b」という「変数名」の代りに「*(&a + (&c-&a)/2)」という「式」が使える */ printf ( "*(&a + (&c-&a)/2) = %d ( b = %d )\n", *(&a + (&c-&a)/2), b ); /* * */ return 0; } /* * */
C:\usr\c>sample-001 &a = bfdb4364 &b = bfdb4368 &c = bfdb436c &c - &a = 2 &a + (&c-&a)/2 = bfdb4368 ( &b = bfdb4368 ) *(&a + (&c-&a)/2) = 222 ( b = 222 ) C:\usr\c>
Download : sample-002.c ( SJIS 版 )
/* * 2014/01/10 sample-002.c */ /* * */ #include <stdio.h> /* * */ int main ( void ) { int a = 345; /* 整数型の変数 a */ int b = 321; /* 整数型の変数 b */ int *p; /* 整数へのポインター型の変数 p */ /* * */ p = &a; /* p に変数 a のポインタ値を代入 (p が a を指す) */ /* * */ printf ( "p = %x (&a = %x)\n", p, &a ); /* p の値 */ printf ( "*p = %d (a=%d, b=%d)\n", *p, a, b ); /* *p の値は 変数 a の値と同じ */ /* * */ a = 666; /* 変数 a の値を変更 */ printf ( "*p = %d (a=%d, b=%d)\n", *p, a, b ); /* *p の値は 変数 a の値と同じ */ /* * */ *p = 789; /* *p を経由して変数 a の値を変更 */ printf ( "*p = %d (a=%d, b=%d)\n", *p, a, b ); /* 変数 a の値と同じ */ printf ( "p = %x (&a = %x)\n", p, &x ); /* p の値が変るわけではない */ /* * */ p = &b; /* 今度は p が b を指す (pの値が変化) */ printf ( "*p = %d (a=%d, b=%d)\n", *p, a, b ); /* 変数 b の値と同じ */ /* * */ *p = 111; /* *p を経由して変数 b の値を変更 */ printf ( "*p = %d (a=%d, b=%d)\n", *p, a, b ); /* 変数 b の値が変化 */ /* * */ return 0; } /* * */
C:\usr\c>sample-002 &a = bf90b564 &b = bf90b568 &c = bf90b56c &c - &a = 2 &a + (&c-&a)/2 = bf90b568 ( &b = bf90b568 ) *(&a + (&c-&a)/2) = 222 ( b = 222 ) C:\usr\c>
Download : sample-003.c ( SJIS 版 )
/* * 2014/01/10 sample-003.c */ /* * */ #include <stdio.h> /* * */ void print_and_inc_with_pointer ( int *p ) { /* * ポインター値も「値」なので、引数に渡す事ができる */ /* 引数で指定されたポインタで指す変数の値を表示し、10 倍にする */ printf ( "(P) Pre : *p = %d ( p = %x )\n", *p, p ); /* p は &a 等と同じ */ *p = *p * 10; /* 10 倍する */ printf ( "(P) Post : *p = %d ( p = %x )\n", *p, p ); } /* * */ void print_and_inc_without_pointer ( int v ) { /* 引数で指定された値は、仮引数変数 (v) にコピーされる */ /* 引数で指定された値を表示し、10 倍にする */ printf ( "(V) Pre : v = %d ( &v = %x )\n", v, &v ); /* &v は &a と違う */ v = v * 10; /* 10 倍する : 変更されるのはコピーの方 */ printf ( "(V) Post : v = %d ( &v = %x )\n", v, &v ); } /* * */ int main ( void ) { int a = 12; int b = 78; int *p = &a; /* * */ printf ( "1: a = %d, b = %d, *p = %d(p=%x)\n\n", a, b, *p, p ); /* 最初 */ a = a * 10; /* a も b を 10 倍に */ b = b * 10; printf ( "2: a = %d, b = %d, *p = %d(p=%x)\n\n", a, b, *p, p ); *p = *p * 10; /* ポインタ経由で a だけ 10 倍に */ printf ( "3: a = %d, b = %d, *p = %d(p=%x)\n\n", a, b, *p, p ); printf ( "*p without pointer\n" ); print_and_inc_without_pointer ( *p ); printf ( "4: a = %d, b = %d, *p = %d(p=%x)\n\n", a, b, *p, p ); printf ( "p with pointer\n" ); print_and_inc_with_pointer ( p ); /* &a でも OK */ printf ( "5: a = %d, b = %d, *p = %d(p=%x)\n\n", a, b, *p, p ); printf ( "b with pointer\n" ); print_and_inc_with_pointer ( &b ); printf ( "6: a = %d, b = %d, *p = %d(p=%x)\n\n", a, b, *p, p ); /* * */ return 0; } /* * */
C:\usr\c>sample-003 1: a = 12, b = 78, *p = 12(p=bf8d4ca4) 2: a = 120, b = 780, *p = 120(p=bf8d4ca4) 3: a = 1200, b = 780, *p = 1200(p=bf8d4ca4) *p without pointer (V) Pre : v = 1200 ( &v = bf8d4c80 ) (V) Post : v = 12000 ( &v = bf8d4c80 ) 4: a = 1200, b = 780, *p = 1200(p=bf8d4ca4) p with pointer (P) Pre : *p = 1200 ( p = bf8d4ca4 ) (P) Post : *p = 12000 ( p = bf8d4ca4 ) 5: a = 12000, b = 780, *p = 12000(p=bf8d4ca4) b with pointer (P) Pre : *p = 780 ( p = bf8d4ca8 ) (P) Post : *p = 7800 ( p = bf8d4ca8 ) 6: a = 12000, b = 7800, *p = 12000(p=bf8d4ca4) C:\usr\c>
Download : sample-004.c ( SJIS 版 )
/* * 2014/01/10 sample-004.c */ /* * */ #include <stdio.h> /* * */ int main ( void ) { int a0 = 10; int a1 = 11; int a2 = 12; int a3 = 13; int a4 = 14; int *p; int i; printf ( "&a0 = %x\n", &a0 ); printf ( "&a1 = %x\n", &a1 ); printf ( "&a2 = %x\n", &a2 ); printf ( "&a3 = %x\n", &a3 ); printf ( "&a4 = %x\n", &a4 ); printf ( "a2 = %d\n", a2 ); p = &a0; for ( i = 0; i < 5; i++ ) { *p = *p * 100; /* p の指す場所を 100 倍する */ printf ( "p = %x\n", p ); p = p + ((&a4 - &a0)/4); /* p の指す場所を一つずらす */ } printf ( "a0 = %d\n", a0 ); printf ( "a2 = %d\n", a2 ); printf ( "a3 = %d\n", a3 ); return 0; } /* * */
C:\usr\c>sample-004 &a0 = bffdaba4 &a1 = bffdaba8 &a2 = bffdabac &a3 = bffdabb0 &a4 = bffdabb4 a2 = 12 p = bffdaba4 p = bffdaba8 p = bffdabac p = bffdabb0 p = bffdabb4 a0 = 1000 a2 = 1200 a3 = 1300 C:\usr\c>
Download : sample-005.c ( SJIS 版 )
/* * 2014/01/10 sample-005.c */ /* * */ #include <stdio.h> /* * */ int main ( void ) { int a[5] = { 10, 11, 12, 13, 14 }; int *p; int i; printf ( "&a[0] = %x\n", &a[0] ); printf ( "&a[1] = %x\n", &a[1] ); printf ( "&a[2] = %x\n", &a[2] ); printf ( "&a[3] = %x\n", &a[3] ); printf ( "&a[4] = %x\n", &a[4] ); printf ( "a[2] = %d\n", a[2] ); p = &a[0]; for ( i = 0; i < 5; i++ ) { *p = *p * 100; /* p の指す場所を 100 倍する */ printf ( "p = %x\n", p ); p = p + ((&a[4] - &a[0])/4); /* p の指す場所を一つずらす */ } printf ( "a[0] = %d\n", a[0] ); printf ( "a[2] = %d\n", a[2] ); printf ( "a[3] = %d\n", a[3] ); return 0; } /* * */
C:\usr\c>sample-005 &a[0] = bf9ccd94 &a[1] = bf9ccd98 &a[2] = bf9ccd9c &a[3] = bf9ccda0 &a[4] = bf9ccda4 a[2] = 12 p = bf9ccd94 p = bf9ccd98 p = bf9ccd9c p = bf9ccda0 p = bf9ccda4 a[0] = 1000 a[2] = 1200 a[3] = 1300 C:\usr\c>
Download : sample-006.c ( SJIS 版 )
/* * 2014/01/10 sample-006.c */ /* * */ #include <stdio.h> /* * */ int main ( void ) { int a[5] = { 10, 11, 12, 13, 14 }; int *p; int i; printf ( "&a[0] = %x\n", &a[0] ); printf ( "&a[1] = %x\n", &a[1] ); printf ( "&a[2] = %x\n", &a[2] ); printf ( "&a[3] = %x\n", &a[3] ); printf ( "&a[4] = %x\n", &a[4] ); printf ( "a[2] = %d\n", a[2] ); p = a; /* a = &a[0] */ for ( i = 0; i < 5; i++ ) { *p = *p * 100; /* p の指す場所を 100 倍する */ printf ( "p = %x\n", p ); p = p + 1; /* ((&a[4] - &a[0])/4) == 1 が保証される */ } printf ( "a[0] = %d\n", a[0] ); printf ( "a[2] = %d\n", a[2] ); printf ( "a[3] = %d\n", a[3] ); return 0; } /* * */
C:\usr\c>sample-006 &a[0] = bfff6d74 &a[1] = bfff6d78 &a[2] = bfff6d7c &a[3] = bfff6d80 &a[4] = bfff6d84 a[2] = 12 p = bfff6d74 p = bfff6d78 p = bfff6d7c p = bfff6d80 p = bfff6d84 a[0] = 1000 a[2] = 1200 a[3] = 1300 C:\usr\c>
Download : sample-007.c ( SJIS 版 )
/* * 2014/01/10 sample-007.c */ /* * */ #include <stdio.h> /* * */ int main ( void ) { int a[5] = { 10, 11, 12, 13, 14 }; int *p; int i; printf ( "&a[0] = %x\n", &a[0] ); printf ( "&a[1] = %x\n", &a[1] ); printf ( "&a[2] = %x\n", &a[2] ); printf ( "&a[3] = %x\n", &a[3] ); printf ( "&a[4] = %x\n", &a[4] ); printf ( "a[2] = %d\n", a[2] ); p = a; /* a = &a[0] */ for ( i = 0; i < 5; i++ ) { *(p+i) = *(p+i) * 100; /* p + i の指す場所を 100 倍する */ printf ( "p + i = %x\n", p + i ); /* p を増やさなくても i が増えれば良い */ } printf ( "a[0] = %d\n", a[0] ); printf ( "a[2] = %d\n", a[2] ); printf ( "a[3] = %d\n", a[3] ); return 0; } /* * */
C:\usr\c>sample-007 &a[0] = bfefc3c4 &a[1] = bfefc3c8 &a[2] = bfefc3cc &a[3] = bfefc3d0 &a[4] = bfefc3d4 a[2] = 12 p + i = bfefc3c4 p + i = bfefc3c8 p + i = bfefc3cc p + i = bfefc3d0 p + i = bfefc3d4 a[0] = 1000 a[2] = 1200 a[3] = 1300 C:\usr\c>
Download : sample-008.c ( SJIS 版 )
/* * 2014/01/10 sample-008.c */ /* * */ #include <stdio.h> /* * */ int main ( void ) { int a[5] = { 10, 11, 12, 13, 14 }; int i; printf ( "&a[0] = %x\n", &a[0] ); printf ( "&a[1] = %x\n", &a[1] ); printf ( "&a[2] = %x\n", &a[2] ); printf ( "&a[3] = %x\n", &a[3] ); printf ( "&a[4] = %x\n", &a[4] ); printf ( "a[2] = %d\n", a[2] ); for ( i = 0; i < 5; i++ ) { *(a+i) = *(a+i) * 100; /* a + i の指す場所を 100 倍する */ printf ( "a + i = %x\n", a + i ); /* p を変えないなら、いらない (a でよい) */ } printf ( "a[0] = %d\n", a[0] ); printf ( "a[2] = %d\n", a[2] ); printf ( "a[3] = %d\n", a[3] ); return 0; } /* * */
C:\usr\c>sample-008 &a[0] = bf8cadc8 &a[1] = bf8cadcc &a[2] = bf8cadd0 &a[3] = bf8cadd4 &a[4] = bf8cadd8 a[2] = 12 a + i = bf8cadc8 a + i = bf8cadcc a + i = bf8cadd0 a + i = bf8cadd4 a + i = bf8cadd8 a[0] = 1000 a[2] = 1200 a[3] = 1300 C:\usr\c>
Download : sample-009.c ( SJIS 版 )
/* * 2014/01/10 sample-009.c */ /* * */ #include <stdio.h> /* * */ int main ( void ) { int a[5] = { 10, 11, 12, 13, 14 }; int i; printf ( "a[2] = %d\n", a[2] ); for ( i = 0; i < 5; i++ ) { a[i] = a[i] * 100; /* a[i] == *(a+i) なので.. */ /* ポインターの事は忘れてよい */ } printf ( "a[0] = %d\n", a[0] ); printf ( "a[2] = %d\n", a[2] ); printf ( "a[3] = %d\n", a[3] ); return 0; } /* * */
C:\usr\c>sample-009 a[2] = 12 a[0] = 1000 a[2] = 1200 a[3] = 1300 C:\usr\c>
Download : sample-010.c ( SJIS 版 )
/* * 2014/01/10 sample-010.c */ /* * */ #include <stdio.h> /* * */ void nat ( int n ) { if ( n > 0 ) { /* 整数が 0 より大きいなら自然数 */ printf ( "%d は自然数です\n", n ); /* 大学数学では 0 を自然数とする事も多いが、まあ今回は高校数学で.. */ } else { printf ( "%d は自然数ではありません\n", n ); } } /* * */ void sign ( int n ) { if ( n < 0 ) { /* 0 より小さい */ printf ( "%d の符号は - です\n", n ); } else if ( n > 0 ) { /* 0 より大きい */ printf ( "%d の符号は + です\n", n ); } else { printf ( "%d の符号は +- です\n", n ); } } /* * */ void week_1 ( int w ) { printf ( "Week 1 : " ); if ( w == 0 ) { printf ( "日曜日\n" ); } else if ( w == 1 ) { printf ( "月曜日\n" ); } else if ( w == 2 ) { printf ( "火曜日\n" ); } else if ( w == 3 ) { printf ( "水曜日\n" ); } else if ( w == 4 ) { printf ( "木曜日\n" ); } else if ( w == 5 ) { printf ( "金曜日\n" ); } else if ( w == 6 ) { printf ( "土曜日\n" ); } else { printf ( "わかりません\n" ); } } /* * */ void week_2 ( int w ) { printf ( "Week 2 : " ); switch ( w ) { case 0: printf ( "日曜日\n" ); break; case 1: printf ( "月曜日\n" ); break; case 2: printf ( "火曜日\n" ); break; case 3: printf ( "水曜日\n" ); break; case 4: printf ( "木曜日\n" ); break; case 5: printf ( "金曜日\n" ); break; case 6: printf ( "土曜日\n" ); break; default: printf ( "わかりません\n" ); break; } } /* * */ void time_zone ( int time ) { printf ( "%d 時は", time ); switch ( time ) { case 18: case 19: case 20: case 21: case 22: case 23: case 0: case 1: case 2: case 3: printf ( "夜" ); break; case 4: case 5: case 6: case 7: case 8: case 9: case 10: printf ( "朝" ); break; case 11: case 12: case 13: case 14: case 15: case 16: case 17: printf ( "昼" ); break; default: printf ( "不明" ); break; } printf ( "です\n" ); } /* * */ int main ( void ) { /* * */ nat ( 3 ); nat ( -10 ); nat ( 0 ); /* * */ sign ( 3 ); sign ( -10 ); sign ( 0 ); /* * */ week_1 ( 1 ); week_1 ( 5 ); week_1 ( 100 ); week_2 ( 1 ); week_2 ( 5 ); week_2 ( 100 ); /* * */ time_zone ( 9 ); time_zone ( 12 ); time_zone ( 18 ); time_zone ( 100 ); /* * */ return 0; } /* * */
C:\usr\c>sample-010 3 は自然数です -10 は自然数ではありません 0 は自然数ではありません 3 の符号は + です -10 の符号は - です 0 の符号は +- です Week 1 : 月曜日 Week 1 : 金曜日 Week 1 : わかりません Week 2 : 月曜日 Week 2 : 金曜日 Week 2 : わかりません 9 時は朝です 12 時は昼です 18 時は夜です 100 時は不明です C:\usr\c>
Download : sample-011.c ( SJIS 版 )
/* * 2014/01/10 sample-011.c */ /* * */ #include <stdio.h> /* * */ int print_and_return ( int value ) { /* 引数で渡された整数値を表示して、その値を結果として返す */ printf ( "PAR: value = %d\n", value ); return value; } /* * */ void print_two_value ( int first, int second ) { /* 引数で指定された二つの値を表示する */ printf ( "PTV: first = %d\n", first ); printf ( "PTV: second = %d\n", second ); } /* * */ int main ( void ) { /* * */ printf ( "call print_and_return = %d\n", print_and_return ( 123 ) ); printf ( "-----\n" ); /* * */ print_two_value ( 444, 555 ); printf ( "-----\n" ); /* * */ print_two_value ( print_and_return ( 678 ), print_and_return ( 999 ) ); /* * */ return 0; } /* * */
C:\usr\c>sample-011 PAR: value = 123 call print_and_return = 123 ----- PTV: first = 444 PTV: second = 555 ----- PAR: value = 999 PAR: value = 678 PTV: first = 678 PTV: second = 999 C:\usr\c>
Download : sample-012.c ( SJIS 版 )
/* * 2014/01/10 sample-012.c */ /* * */ #include <stdio.h> /* * */ void nPrint ( int n, ... ) { /* 複数の整数を引数にして出力 */ int *p = &n; int i; for ( i = 0; i < n; i++ ) { printf ( "%d'th arg = %d\n", i + 1, p[i+1] ); /* p[0] は n そのもの、最初の引数は p[1] == p[0+1] になる */ } } /* * */ int main ( void ) { /* * */ nPrint ( 2, 123, 456 ); printf ( "---\n" ); nPrint ( 5, 100, 120, 140, 160, 180 ); printf ( "---\n" ); nPrint ( 2, 111, 222, 333, 444 ); printf ( "---\n" ); nPrint ( 3, 9 ); /* * */ return 0; } /* * */
C:\usr\c>sample-012 1'th arg = 123 2'th arg = 456 --- 1'th arg = 100 2'th arg = 120 3'th arg = 140 4'th arg = 160 5'th arg = 180 --- 1'th arg = 111 2'th arg = 222 --- 1'th arg = 9 2'th arg = 222 3'th arg = 333 C:\usr\c>
Download : sample-013.c ( SJIS 版 )
/* * 2014/01/10 sample-013.c */ /* * */ #include <stdio.h> /* * */ void nPrint ( int n, ... ) { /* 複数の整数を引数にして出力 */ int *p = &n; int i; for ( i = 0; i < n; i++ ) { printf ( "%d'th arg = %d (%x)\n", i + 1, p[i+1], p + (i + 1) ); /* p[0] は n そのもの、最初の引数は p[1] == p[0+1] になる */ } } /* * */ int main ( void ) { /* * */ nPrint ( 2, 123, 456 ); printf ( "---\n" ); nPrint ( 5, 100, 120, 140, 160, 180 ); printf ( "---\n" ); nPrint ( 2, 111, 222, 333, 444 ); printf ( "---\n" ); nPrint ( 3, 9 ); /* * */ return 0; } /* * */
C:\usr\c>sample-013 1'th arg = 123 (bfbbc7f4) 2'th arg = 456 (bfbbc7f8) --- 1'th arg = 100 (bfbbc7f4) 2'th arg = 120 (bfbbc7f8) 3'th arg = 140 (bfbbc7fc) 4'th arg = 160 (bfbbc800) 5'th arg = 180 (bfbbc804) --- 1'th arg = 111 (bfbbc7f4) 2'th arg = 222 (bfbbc7f8) --- 1'th arg = 9 (bfbbc7f4) 2'th arg = 222 (bfbbc7f8) 3'th arg = 333 (bfbbc7fc) C:\usr\c>