01 #include <stdio.h> 02 int main() 03 { 04 int x1[] = {1, 2, 3}, x2[3], *x3, *x4 = new int [3]; 05 x3 = x2; // x3 = &x2[0]; 06 for (int i1 = 0; i1 < 3; i1++) { 07 x2[i1] = x1[i1]; // x2 = x1; は許されない 08 x4[i1] = 10 * (i1 + 1); 09 } 10 for (int i1 = 0; i1 < 3; i1++) 11 printf("i1 %d x1 %d x2 %d x3 %d x4 %d\n", i1, x1[i1], x2[i1], x3[i1], x4[i1]); 12 x3 = x4; 13 for (int i1 = 0; i1 < 3; i1++) 14 printf("i1 %d x1 %d x2 %d x3 %d x4 %d\n", i1, x1[i1], x2[i1], x3[i1], x4[i1]); 15 return 0; 16 }
i1 0 x1 1 x2 1 x3 1 x4 10 i1 1 x1 2 x2 2 x3 2 x4 20 i1 2 x1 3 x2 3 x3 3 x4 30
i1 0 x1 1 x2 1 x3 10 x4 10 i1 1 x1 2 x2 2 x3 20 x4 20 i1 2 x1 3 x2 3 x3 30 x4 30
// int x[5000000]; // 実行できない // int *x = (int *)malloc(5000000 * sizeof(int)); // 実行可 int *x = new int [5000000]; // 実行可 x[4999999] = 100; printf("%d\n", x[4999999]);
01 #include <stdio.h> 02 int main() 03 { 04 int x1[][2] = {{1, 2}, {3, 4}, {5, 6}}, *x2, **x3, **x4; 05 x2 = &x1[0][0]; // x2 = x1[0]; でも可 06 // m 行 n 列の (i, j) 要素の位置: i * n + j 07 for (int i1 = 0; i1 < 3; i1++) { 08 for (int i2 = 0; i2 < 2; i2++) 09 printf("x1 %d x2 %d\n", x1[i1][i2], x2[i1*2+i2]); 10 } 11 x3 = new int *[3]; 12 for (int i1 = 0; i1 < 3; i1++) 13 x3[i1] = new int [2]; 14 x4 = x3; 15 x3[0][0] = 10; 16 x3[0][1] = 20; 17 x3[1][0] = 30; 18 x3[1][1] = 40; 19 x3[2][0] = 40; 20 x3[2][1] = 50; 21 for (int i1 = 0; i1 < 3; i1++) { 22 for (int i2 = 0; i2 < 2; i2++) 23 printf("x3 %d x4 %d\n", x3[i1][i2], x4[i1][i2]); 24 } 25 // 以下は許されない(x3[0][0],x3[0][1] だけは参照できる) 26 int *x5 = &x3[0][0]; // int *x5 = x3[0]; でも可 27 for (int i1 = 0; i1 < 3; i1++) { 28 for (int i2 = 0; i2 < 2; i2++) 29 printf("x3 %d x5 %d\n", x3[i1][i2], x5[i1*2+i2]); 30 } 31 return 0; 32 }
x1 1 x2 1 x1 2 x2 2 x1 3 x2 3 x1 4 x2 4 x1 5 x2 5 x1 6 x2 6
x3 10 x4 10 x3 20 x4 20 x3 30 x4 30 x3 40 x4 40 x3 40 x4 40 x3 50 x4 50
x3 10 x5 10 x3 20 x5 20 x3 30 x5 1628620015 x3 40 x5 134280023 x3 40 x5 12594745 x3 50 x5 0