1、目标单元与简介存取
直接访问和间接访问
c#include <stdio.h> int main(void) { int a = 3, *p; p = &a; printf("a = %d, *p = %d\n", a, *p); *p = 10; printf("a = %d, *p = %d\n", a, *p); printf("Enter a: "); scanf("%d", &a); printf("a = %d, *p = %d\n", a, *p); (*p)++; printf("a = %d, *p = %d\n", a, *p); return 0; }
- 运行结果:
2、引用指针变量
引用指针变量
c#include <stdio.h> int main() { int a = 15; int *p = &a; printf("%d, %d\n", a, *p); return 0; }
- 运行结果:
通过指针修改内存上的数据
c#include <stdio.h> int main() { int a = 15, b = 99, c = 222; int *p = &a; *p = b; c = *p; printf("%d, %d, %d, %d\n", a, b, c, *p); return 0; }
- 运行结果:
3、指针变量作为函数参数
通过指针交换两个变量的值:
c#include <stdio.h> void swap(int *p1, int *p2) { int temp; temp = *p1; *p1 = *p2; *p2 = temp; } int main() { int a = 66, b = 99; swap(&a, &b); printf("a = %d, b = %d\n", a, b); return 0; }
- 运行结果:
4、指向数组的指针
通过指针输出数组中元素的值
c#include <stdio.h> int main(void) { int i, a[] = {1, 3, 5, 7, 9}; int *p = a; for (i = 0; i < 5; i++) { printf("%d\t", *(p+i)); } printf("\n"); return 0; }
- 运行结果:
5、指向数组的指针作为函数参数
利用数组名作为参数,将数组中的 10 个整数完全颠倒顺序:
c#include <stdio.h> void inv(int *x, int n); int main() { int i, a[] = {3, 7, 9, 11, 0, 6, 7, 5, 4, 2}; printf("The original array: \n"); for (i = 0; i < 10; i++) { printf("%3d", a[i]); } printf("\n"); inv(a, 10); printf("The array has been inverted: \n"); for (i = 0; i < 10; i++) { printf("%3d", a[i]); } printf("\n"); return 0; } void inv(int *x, int n) { int t, *i, *j; for (i = x, j = x + n - 1; i <= j; i++, j--) { t = *i; *i = *j; *j = t; } return; }
- 运行结果:
6、字符串指针
八进制转换成十进制:
c#include <stdio.h> int main(void) { char *p, s[6]; int n; n = 0; p = s; printf("Enter the octal number you want to convert: \n"); gets(p); while (*(p) != '\0') { n = n * 8 + *p - '0'; p++; } printf("The converted decimal number is: \n%d\n", n); return 0; }
- 运行结果:
7、指针复制字符串
字符串复制:
c#include <stdio.h> int main(void) { char str1[10], str2[10]; char *p1, *p2; p1 = str1; p2 = str2; printf("Please enter the original string: \n"); gets(p2); for (; *p2 != '\0'; p1++, p2++) { *p1 = *p2; } *p1 = '\0'; printf("The original string is %s\n, and the copied string is %s\n", str2, str1); return 0; }
- 运行结果:
字符串连接:
c#include <stdio.h> int main(void) { char str1[10], str2[10], str[10]; char *p1, *p2, *p; int i = 0; p1 = str1; p2 = str2; p = str; printf("Please enter the str1: \n"); gets(p1); printf("Please enter the str2: \n"); gets(p2); while (*p1 != '\0') { *p = *p1; p += 1; p1 += 1; i++; } for (; *p2 != '\0'; p1++, p2++, p++) { *p = *p2; } *p = '\0'; printf("str1 is %s\nstr2 is %s\nAfter connection is: %s\n", str1, str2, str); return 0; }
- 运行结果:
已知一个字符串,使用返回指针的函数,实现把该字符串中的 '*' 号删除,同时把后面连接的字符串前移
c#include <stdio.h> #include <string.h> char *strarrange(char *arr) { char *p = arr; char *t; while (*p != '\0') { p++; if (*p == '*') { t = p; while (*t != '\0') { *t = *(t + 1); t++; } p--; } } return arr; } int main(void) { char s[] = "abc*def***ghi*jklmn"; char *p; p = s; printf("Before deletion, the character string is: %s\n", p); printf("After deletion, the character string is: %s\n", strarrange(p)); return 0; }
- 运行结果:
8、函数指针
指向函数的指针:
c#include <stdio.h> int max(int x, int y) { int z; if (x > y) { z = x; } else { z = y; } return z; } int main(void) { int(*p)(int, int); int a, b, c; p = max; printf("Enter the values of a and b\n"); scanf("%d %d", &a, &b); c = (*p)(a, b); printf("The larger value of %d and %d is: %d\n", a, b, c); return 0; }
- 运行结果:
9、使用 const 修饰指针变量
const 类型 *变量名
:可以改变指针的指向,不能改变指针指向的内容类型 * const 变量名
:可以改变指针指向的内容,不能改变指针的指向const 类型 * const 变量名
:指针的指向、指针指向的内容都不可以改变
10、数值排序
实现将 3 个数值进行降序排列:
c#include <stdio.h> #include <stdlib.h> void fun(int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; } void exchange(int *a, int *b, int *c) { if (*a < *b) fun(a, b); if (*a < *c) fun(a, c); if (*b < *c) fun(b, c); } void main() { int *p1 = (int*)malloc(sizeof(int)); int *p2 = (int*)malloc(sizeof(int)); int *p3 = (int*)malloc(sizeof(int)); printf("Please input 3 numbers: \n"); scanf("%d %d %d", p1, p2, p3); exchange(p1, p2, p3); printf("Output: \n"); printf("*p1 = %d\t*p2 = %d\t*p3 = %d\n", *p1, *p2, *p3); free(p1); free(p2); free(p3); }
- 运行结果: