C 语言学习-06【指针】

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);
}
  • 运行结果:
相关推荐
低头专研3 小时前
Markdown标题序号处理工具——用 C 语言实现
c语言·开发语言·typora·markdown文件标题编号·md文件标题序号
viperrrrrrrrrr75 小时前
大数据学习(105)-Hbase
大数据·学习·hbase
weixin_428498496 小时前
Visual Studio 中使用 Clang 作为 C/C++ 编译器时,设置优化选项方法
c语言·c++·visual studio
菜鸡中的奋斗鸡→挣扎鸡6 小时前
第十四届蓝桥杯大赛软件赛省赛C/C++ 大学 B 组
c语言·c++·蓝桥杯
南玖yy6 小时前
探索 C 语言数据结构:从基础到实践
c语言·开发语言·数据结构
前进的程序员7 小时前
Linux C 与 C 语言的区别及开发差异
linux·运维·c语言
行思理7 小时前
go语言应该如何学习
开发语言·学习·golang
しかし1181148 小时前
C语言队列的实现
c语言·开发语言·数据结构·数据库·经验分享·链表
oceanweave8 小时前
【k8s学习之CSI】理解 LVM 存储概念和相关操作
学习·容器·kubernetes
杰杰批9 小时前
第十四届蓝桥杯大赛软件赛国赛C/C++研究生组
c语言·c++·蓝桥杯