一方,リスト2では,呼び出された関数は呼出元の変数を操作
している.呼び出された側の関数は呼出元の変数のアドレスを知っているから,呼出元の
変数の操作ができる.
1 #include <stdio.h>
2 void swap(int a, int b);
3
4 //--------------------------------------------
5 int main(void){
6 int hoge=1111, fuga=2222;
7
8 printf("hoge=%d\tfuga=%d\n",hoge,fuga);
9 swap(hoge, fuga);
10 printf("hoge=%d\tfuga=%d\n",hoge,fuga);
11
12 return 0;
13 }
14
15 //-------------------------------------------
16 void swap(int a, int b){
17 int temp;
18
19 temp=b;
20 b=a;
21 a=temp;
22 }
hoge=1111 fuga=2222 hoge=1111 fuga=2222
1 #include <stdio.h>
2 void swap(int *a, int *b);
3
4 //--------------------------------------------
5 int main(void){
6 int hoge=1111, fuga=2222;
7
8 printf("hoge=%d\tfuga=%d\n",hoge,fuga);
9 swap(&hoge, &fuga);
10 printf("hoge=%d\tfuga=%d\n",hoge,fuga);
11
12 return 0;
13 }
14
15 //-------------------------------------------
16 void swap(int *a, int *b){
17 int temp;
18
19 temp=*b;
20 *b=*a;
21 *a=temp;
22 }
hoge=1111 fuga=2222 hoge=2222 fuga=1111
1 #include <stdio.h>
2
3 void cal(int *wa, int *sa, int *seki, int *sho, int a, int b);
4
5 //----------------------------------------------------------
6 int main(void){
7 int add, sub, mul, div;
8
9 cal(&add, &sub, &mul, &div, 33, 3);
10 printf("add = %d\n", add);
11 printf("sub = %d\n", sub);
12 printf("mul = %d\n", mul);
13 printf("div = %d\n", div);
14
15 return 0;
16 }
17
18 //----------------------------------------------------------
19 void cal(int *wa, int *sa, int *seki, int *sho, int a, int b){
20
21 *wa = a+b;
22 *sa = a-b;
23 *seki = a*b;
24 *sho = a/b;
25 }
add = 36 sub = 30 mul = 99 div = 11
リスト4の関数reverse()は,配列に格納されている順序を逆に
する関数である.動作については,各自考えよ.
1 #include <stdio.h>
2 void reverse(int n, int a[]);
3
4 //--------------------------------------------
5 int main(void){
6 int hoge[3]={1,2,3};
7
8 printf("hoge=\t%d\t%d\t%d\n",hoge[0],hoge[1],hoge[2]);
9 reverse(3, hoge);
10 printf("hoge=\t%d\t%d\t%d\n",hoge[0],hoge[1],hoge[2]);
11
12 return 0;
13 }
14
15 //-------------------------------------------
16 void reverse(int n, int a[]){
17 int i, i_max, temp;
18
19 i_max=n/2;
20
21 for(i=0; i<i_max; i++){
22 temp=a[i];
23 a[i]=a[n-1-i];
24 a[n-1-i]=temp;
25 }
26 }
hoge= 1 2 3 hoge= 3 2 1
配列の先頭アドレスから添字に対応するデータのアドレスの計算方法は,先週の講義ノートを見よ.
1 #include <stdio.h>
2 void reverse(int m, int n, int a[][3]);
3
4 //--------------------------------------------
5 int main(void){
6 int hoge[2][3]={{11,12,13},{21,22,23}};
7
8 printf("%d\t%d\t%d\n",hoge[0][0],hoge[0][1],hoge[0][2]);
9 printf("%d\t%d\t%d\n",hoge[1][0],hoge[1][1],hoge[1][2]);
10 reverse(2, 3, hoge);
11 printf("\n");
12 printf("%d\t%d\t%d\n",hoge[0][0],hoge[0][1],hoge[0][2]);
13 printf("%d\t%d\t%d\n",hoge[1][0],hoge[1][1],hoge[1][2]);
14
15 return 0;
16 }
17
18 //-------------------------------------------
19 void reverse(int m, int n, int a[][3]){
20 int i, j, j_max, temp;
21
22 j_max=n/2;
23
24 for(i=0; i<m; i++){
25 for(j=0; j<j_max; j++){
26 temp = a[i][j];
27 a[i][j] = a[i][n-1-j];
28 a[i][n-1-j] = temp;
29 }
30 }
31 }
11 12 13 21 22 23 13 12 11 23 22 21
もちろん,配列に格納された文字列を渡すこともできる.それは前節の整数が格納された配
列と同じようにすればよい.
1 #include <stdio.h>
2
3 void my_print(char *str); // プロトタイプ宣言
4 //------------------------------------------------
5 int main(void){
6 char *s;
7
8 s="Akita National College of Technology";
9
10 my_print(s);
11
12 return 0;
13 }
14
15 //------------------------------------------------
16 void my_print(char *str){
17
18 printf("ここは,%s です.\n", str);
19
20 }
ここは,Akita National College of Technology です.