1 // 阵列的复制代价很大,不是一个=赋值就可以的 ,得逐个元素复制过来 2 3 #include<stdio.h> 4 #include<stdlib.h> 5 #include<time.h> 6 7 int main() 8 { 9 srand(time(0)); 10 int i, n[10], v[10]; 11 for (i = 0; i < 10; i++) { 12 n[i] = rand() % 100; // 在0-99之间随机取10个数 13 14 } 15 for (i = 0; i < 10; i++) { 16 v[i] = n[i]; 17 18 } 19 for (i = 0; i < 10; i++) { 20 printf("%d ", n[i]); 21 22 } 23 printf(" "); 24 for (i = 0; i < 10; i++) { 25 printf("%d ", v[i]); 26 } 27 printf(" "); 28 return 0; 29 } 30 31 47 96 20 91 15 98 89 9 22 85 32 47 96 20 91 15 98 89 9 22 85 33 34 Process returned 0 (0x0) execution time : 15.246 s 35 Press any key to continue.
用呼叫函式印出两个阵列
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 5 void printArray(int [10]); 6 7 int main() 8 { 9 srand(time(0)); 10 int i, n[10], v[10]; 11 for (i = 0; i < 10; i++) { 12 n[i] = rand() % 100; 13 } 14 for (i = 0; i < 10; i++) { 15 v[i] = n[i]; // 复制 16 } 17 printArray(n); 18 printArray(v); 19 return 0; 20 21 } 22 23 void printArray(int t[10]) { 24 int i; 25 for (i = 0; i < 10; i++) { 26 printf(" %d", t[i]); 27 } 28 printf(" "); 29 } 30 31 36 95 2 25 69 21 14 85 12 15 32 36 95 2 25 69 21 14 85 12 15 33 34 Process returned 0 (0x0) execution time : 2.709 s 35 Press any key to continue.
阵列呼叫函式与其他个别呼叫的区别:
阵列呼叫函式后,对函式内改变后,改变的是其本尊.
一般整数呼叫后,没有改变.
1 // 阵列呼叫函式与其他呼叫函数区别 2 3 #include <stdio.h> 4 void increase (int [3]); 5 6 int main() { 7 int i, n[3] = {1, 2, 3}; 8 increase(n); 9 for (i = 0; i < 3; i++) { 10 printf("n[%d] = %d ", i, n[i]); 11 } 12 return 0; 13 } 14 15 void increase(int n[3]) { 16 int i; 17 for (i = 0; i < 3; i++) { 18 n[i]++; 19 } 20 } 21 22 n[0] = 2 23 n[1] = 3 24 n[2] = 4 25 26 Process returned 0 (0x0) execution time : 10.484 s 27 Press any key to continue. 28 29 30 #include<stdio.h> 31 32 void increase(int); 33 34 int main() { 35 int n = 1; 36 increase(n); 37 printf("n = %d ", n); 38 return 0; 39 40 } 41 42 void increase(int n) { 43 n++; 44 } 45 46 n = 1 47 48 Process returned 0 (0x0) execution time : 6.315 s 49 Press any key to continue.
19.4sizeof 运算子
资料型别占用的记忆体空间大小大多是属于实作定义,会随着编译器与设定上的不同有所差异
sizeof运算结果为size_t 型别 (在printf时使用zu):我的编译器用%zu无法解读出来,这里我用%d代替,可见在引用函式与没有引用函式数组大小与长度结果是不一样的
1 #include <stdio.h> 2 3 void f(int[]); 4 5 int main() { 6 int v[5] = {1, 2, 3, 4, 5}; 7 f(v); 8 return 0; 9 } 10 11 void f(int v[]) { 12 printf("Size of int: %d ", sizeof(int)); 13 printf("Size of v[0]: %d ", sizeof(v[0])); 14 printf("Size of v: %d ", sizeof(v)); 15 printf("Length of v: %d ", sizeof(v)/sizeof(v[0])); 16 } 17 18 Size of int: 4 19 Size of v[0]: 4 20 Size of v: 4 21 Length of v: 1 22 23 Process returned 0 (0x0) execution time : 5.493 s 24 Press any key to continue. 25 26 #include <stdio.h> 27 28 int main() { 29 int v[5] = {1, 2, 3, 4, 5}; 30 printf("Size of int: %d ", sizeof(int)); 31 printf("Size of v[0]: %d ", sizeof(v[0])); 32 printf("Size of v: %d ", sizeof(v)); 33 printf("Length of v: %d ", sizeof(v)/sizeof(v[0])); 34 return 0; 35 36 } 37 38 Size of int: 4 39 Size of v[0]: 4 40 Size of v: 20 41 Length of v: 5 42 43 Process returned 0 (0x0) execution time : 0.998 s 44 Press any key to continue.
19.2 可对任意大小的阵列求最大值
发现:maxv(a, N)中 N要与数组个数一致
1 #include <stdio.h> 2 3 int maxv(int[], int N); 4 5 int main() { 6 int a[3] = {3, 9, 7}; 7 printf("Max: %d ", maxv(a, 3)); 8 int b[5] = {3, 4, 1, 2, 7}; 9 printf("Max: %d ", maxv(b, 5)); 10 return 0; 11 12 } 13 14 int maxv(int v[], int N) { 15 int max = v[0], i; 16 for (i = 1; i < N; i++) { 17 if (v[i] > max) 18 { 19 max = v[i]; 20 } 21 } 22 23 return max; 24 } 25 26 Max: 9 27 Max: 7 28 29 Process returned 0 (0x0) execution time : 0.965 s 30 Press any key to continue.