1.对输入的两个整数按大小顺序输出。用函数处理,而且用指针类型的数据作函数参数
#include<stdio.h> int main() { void swap(int *p1, int *p2); int a, b; int *pointer_1, *pointer_2; printf("please enter a and b:"); scanf_s("%d,%d", &a, &b); pointer_1 = &a; pointer_2 = &b; if (a < b) swap(pointer_1, pointer_2); printf("max=%d,min=%d\n", a, b); return 0; } void swap(int *p1, int *p2) { int temp; temp = *p1; *p1 = *p2; *p2 = temp; }运行结果:
2.输入3个整数a,b,c,要求按由小到大的顺序将它们输出。用函数实现。
#include<stdio.h> int main() { void exchange(int *q1, int *q2, int *q3); int a, b, c, *p1, *p2, *p3; printf("please enter three numbers:"); scanf_s("%d,%d,%d", &a, &b, &c); p1 = &a; p2 = &b; p3 = &c; exchange(p1, p2, p3); printf("The order is:%d,%d,%d\n", a, b, c); return 0; } void exchange(int *q1, int *q2, int *q3) { void swap(int *pt1, int *pt2); if (*q1 < *q2)swap(q1, q2); if (*q1 < *q3)swap(q1, q3); if (*q2 < *q3)swap(q2, q3); } void swap(int *pt1, int *pt2) { int temp; temp = *pt1; *pt1 = *pt2; *pt2 = temp; }运行结果:
3.通过指针变量输出整型数组a的10个元素
#include<stdio.h> int main() { int i,a[10], *p = a; printf("please enter 10 integer numbers:"); for (i = 0; i < 10; i++) scanf_s("%d", p++); p = a; for (i = 0; i < 10; i++, p++) printf("%d ", *p); printf("\n"); return 0; }运行结果: