C语言精华-指针01
指针变量的引用
代码如下:
//通过指针变量访问整型变量 #include <stdio.h> int main(void) { int a, b, *p1, *p2; a = 100; b = 10; p1 = &a; p2 = &b; printf("a=%d, b=%d\n", a, b); printf("*p1=%d, *p2=%d\n", *p1, *p2); printf("&a=%x, &b=%x\n", &a, &b); printf("p1=%x, p2=%x\n", p1, p2); printf("&p1=%x, &p2=%x\n", &p1, &p2); return 0; }
结果为:
指针变量作为函数参数—地址传递:
【工程案例】将数从大到小输出:
#include <stdio.h> void swap(int x, int y) { int temp; temp = x; x = y; y = temp; } int main(void) { int a, b; scanf_s("%d,%d", &a, &b); if (a<b) swap(a, b); printf("\na=%d,b=%d\n", a, b); return 0; }
值传递!!!!!!!!!!!!!!!!!!!!!!!!
【工程案例】将数组a中n个整数按相反顺序存放。
方法一:
//⑴ 实参与形参均用数组 #include <stdio.h> void inv(int x[], int n); int main() { int i, a[10] = { 3, 7, 9, 11, 0, 6, 7, 5, 4, 2 }; printf("The original array:\n"); for (i = 0; i<10; i++) printf("%4d",a[i]); printf("\n"); inv(a, 10); printf("The array has been inverted:\n"); for (i = 0; i<10; i++) printf("%4d", a[i]); printf("\n"); return 0; } void inv(int x[], int n) { int temp, i, j, m = (n - 1) / 2; for (i = 0; i <= m; i++){ j = n - 1 - i; temp = x[i]; x[i] = x[j]; x[j] = temp; } return; }
结果为:
方法二:
//⑵ 实参用数组,形参用指针变量 #include <stdio.h> void inv(int *x, int n); int main() { int i, a[10] = { 3, 7, 9, 11, 0, 6, 7, 5, 4, 2 }; printf("The original array:\n"); for (i = 0; i<10; i++) printf("%4d",a[i]); printf("\n"); inv(a, 10); printf("The array has been inverted:\n"); for (i = 0; i<10; i++) printf("%4d",a[i]); printf("\n"); return 0; } void inv(int *x, int n) { int temp, *p, *i, *j, m = (n - 1) / 2; i = x; j = x + n - 1; p = x + m; for (; i <= p; i++, j--){ temp = *i; *i = *j; *j = temp; } return; }
结果为:
方法三:
//(3)实参与形参均用指针变量 #include <stdio.h> int main() { void inv(int *x, int n); int i, arr[10], *p = arr; printf("The original array:\n"); for (i = 0; i<10; i++, p++) scanf_s("%d", p); p = arr; inv(p, 10); printf("The array has been inverted:\n"); for (p = arr; p<arr + 10; p++) printf("%4d", *p); printf("\n"); return 0; } void inv(int *x, int n) { int *p, m, temp, *i, *j; m = (n - 1) / 2; i = x; j = x + n - 1; p = x + m; for (; i <= p; i++, j--) { temp = *i; *i = *j; *j = temp; } }
结果为:
方法四:
//⑷ 实参用指针变量,形参用数组 #include <stdio.h> int main() { void inv(int x[], int n); int i, a[10], *p = a; for (i = 0; i<10; i++, p++) scanf_s("%d", p); p = a; inv(p, 10); printf("The array has been inverted:\n"); for (p = a; p<a + 10; p++) printf("%4d", *p); printf("\n"); return 0; } void inv(int x[], int n) { int t, i, j, m = (n - 1) / 2; for (i = 0; i <= m; i++) { j = n - 1 - i; t = x[i]; x[i] = x[j]; x[j] = t; } }
结果同上。
【工程案例】3个学生各学4门课,计算总平均分,输出第n个学生成绩
#include <stdio.h> void average(float *p, int n); void search(float(*p)[4], int n); int main() { float score[3][4] = { { 65, 67, 79, 60 }, { 80, 87, 90, 81 }, { 90, 99, 100, 98 } }; average(*score, 12); search(score, 2); return 0; } void average(float *p, int n) { float *p_end, sum = 0, aver; p_end = p + n - 1; for (; p <= p_end; p++) sum = sum + (*p); aver = sum / n; printf("average=%5.2f\n", aver); } void search(float(*p)[4], int n) { int i; printf("score of No.%d :\n", n); for (i = 0; i < 4; i++) printf("%5.2f ", *(*(p + n) + i)); printf("\n"); }
结果可以自行测试。
总结:二维数组与一维数组指针变量的关系