当前位置: 首页 > news >正文

C 语言学习-06【指针】

1、目标单元与简介存取

直接访问和间接访问

#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、引用指针变量

引用指针变量

#include <stdio.h>int main() {int a = 15;int *p = &a;printf("%d, %d\n", a, *p);return 0;
}
  • 运行结果:
    在这里插入图片描述

通过指针修改内存上的数据

#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、指针变量作为函数参数

通过指针交换两个变量的值:

#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、指向数组的指针

通过指针输出数组中元素的值

#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 个整数完全颠倒顺序:

#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、字符串指针

八进制转换成十进制:

#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、指针复制字符串

字符串复制:

#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;
}
  • 运行结果:
    在这里插入图片描述

字符串连接:

#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;
}
  • 运行结果:
    在这里插入图片描述

已知一个字符串,使用返回指针的函数,实现把该字符串中的 ‘*’ 号删除,同时把后面连接的字符串前移

#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、函数指针

指向函数的指针:

#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 个数值进行降序排列:

#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);
}
  • 运行结果:
    在这里插入图片描述
http://www.lryc.cn/news/491637.html

相关文章:

  • 如何快速将Excel数据导入到SQL Server数据库
  • 【人工智能】Python在机器学习与人工智能中的应用
  • 使用八爪鱼爬虫抓取汽车网站数据,分析舆情数据
  • 什么是事务?事务有哪些特性?
  • 玩转合宙Luat教程 基础篇④——程序基础(库、线程、定时器和订阅/发布)
  • 24.<Spring博客系统①(数据库+公共代码+持久层+显示博客列表+博客详情)>
  • webp 网页如何录屏?
  • 丹摩征文活动|实现Llama3.1大模型的本地部署
  • Spring Boot 2 和 Spring Boot 3 中使用 Spring Security 的区别
  • 【数据结构与算法】 LeetCode:回溯
  • SpringBoot线程池的使用
  • Neural Magic 发布 LLM Compressor:提升大模型推理效率的新工具
  • HttpServletRequest req和前端的关系,req.getParameter详细解释,req.getParameter和前端的关系
  • React-useEffect的使用
  • MySQL数据库与Informix:能否创建同名表?
  • 爬虫实战:采集知乎XXX话题数据
  • 大数据新视界 -- Hive 数据桶原理:均匀分布数据的智慧(上)(9/ 30)
  • 【小白学机器学习33】 大数定律python的 pandas.Dataframe 和 pandas.Series基础内容
  • 【shodan】(五)网段利用
  • LeetCode739. 每日温度(2024冬季每日一题 15)
  • Node.js的http模块:创建HTTP服务器、客户端示例
  • 加菲工具 - 好用免费的在线工具集合
  • .NET9 - 新功能体验(二)
  • map和redis关系
  • 《数据结构》学习系列——图(中)
  • 探索Python的HTTP之旅:揭秘Requests库的神秘面纱
  • Python 爬虫从入门到(不)入狱学习笔记
  • IDEA优雅debug
  • wp the_posts_pagination 与分类页面搭配使用
  • 大数据-231 离线数仓 - DWS 层、ADS 层的创建 Hive 执行脚本