/****************************/ /* 様々な引数 */ /* coded by Y.Suganuma */ /****************************/ #include <stdio.h> #include <vector> using namespace std; // a : int, b : int に対するアドレス渡し, c : int に対する参照渡し,d : 配列 // x : vector, y : vector に対するアドレス渡し, z : vector に対する参照渡し void func(int a, int*b, int &c, int d[], vector<int> x, vector<int> *y, vector<int> &z) { a = 10; *b = 20; c = 30; d[0] = 11; x[0] = 12; (*y)[1] = 13; z[2] = 14; printf("(関数内)\n"); printf("a; %d b; %d c; %d\n", a, *b, c); printf("d:"); for (int i1 = 0; i1 < 3; i1++) printf(" %d", d[i1]); printf(" x:"); for (int i1 = 0; i1 < 3; i1++) printf(" %d", x[i1]); printf(" y:"); for (int i1 = 0; i1 < 3; i1++) printf(" %d", (*y)[i1]); printf(" z:"); for (int i1 = 0; i1 < 3; i1++) printf(" %d", z[i1]); printf("\n"); } int main() { int a = 1, b = 2, c = 3; int d[] = {1, 2, 3}; vector<int> x = {1, 2, 3}; vector<int> y = {4, 5, 6}; vector<int> z = {7, 8, 9}; func(a, &b, c, d, x, &y, z); printf("(関数を呼んだ後)\n"); printf("a; %d b; %d c; %d\n", a, b, c); printf("d:"); for (auto v : d) printf(" %d", v); printf(" x:"); for (auto v : x) printf(" %d", v); printf(" y:"); for (auto v : y) printf(" %d", v); printf(" z:"); for (auto v : z) printf(" %d", v); printf("\n"); return 0; }
(関数内) a; 10 b; 20 c; 30 d: 11 2 3 x: 12 2 3 y: 4 13 6 z: 7 8 14 (関数を呼んだ後) a; 1 b; 20 c; 30 d: 11 2 3 x: 1 2 3 y: 4 13 6 z: 7 8 14