当前位置:网站首页>C语言用指向指针的指针对n个整数排序
C语言用指向指针的指针对n个整数排序
2022-07-29 05:08:00 【cpp编程】
C语言用指向指针的指针的方法对n个整数排序并输出;要求将排序单独写成一个函数;n个整数在主函数中输入,最后在主函数中输出。
解题思路:读者看着道题的时候,首先要观察一下有什么规律,然后指向指针的指针在上一道练习题中已经有了铺垫,读者可以联系上一道题去熟练使用指向指针的指针。
C语言源代码演示:
#include<stdio.h>//头文件
int main()//主函数
{
void sort(int **point,int number); //sort排序函数声明
int i,number,data[20],**point,*pstr[20]; //定义变量
printf("输入要排序的个数number:");//提示语句
scanf("%d",&number);//键盘输入
for(i=0;i<number;i++)
{
pstr[i]=&data[i]; //将第i个整数的地址赋予指针数组pstr的第i个元素
}
printf("逐个输入这%d个数:",number);//提示语句
for(i=0;i<number;i++)
{
scanf("%d",pstr[i]);//挨个输入要排序的数
}
point=pstr;
sort(point,number);
printf("\n-------------------\n"); //提示语句
printf("输出结果:\n");//提示语句
for(i=0;i<number;i++)
{
printf("%d ",*pstr[i]);//输出排序后的结果
}
printf("\n");//换行
return 0;//主函数返回值为0
}
void sort(int **point,int number)//自定义sort排序函数
{
int i,j,*temp;//定义变量
for(i=0;i<number-1;i++)
{
for(j=i+1;j<number;j++)
{
if(**(point+i)>**(point+j))//比较之后交换整数地址
{
temp=*(point+i);
*(point+i)=*(point+j);
*(point+j)=temp;
}
}
}
}
编译运行结果:
输入要排序的个数number:3
逐个输入这3个数:1 8 5
-------------------
输出结果:
1 5 8
--------------------------------
Process exited after 6.278 seconds with return value 0
请按任意键继续. . .
今天的分享就到这里了,大家要好好学C语言/C++哟~
写在最后:对于准备学习C/C++编程的小伙伴,如果你想更好的提升你的编程核心能力(内功)不妨从现在开始!
C语言C++编程学习交流圈子,QQ群:763855696【点击进入】
C语言从入门到精通(C语言入门C语言教程C语言零基础C语言基础C语言学习C
整理分享(多年学习的源码、项目实战视频、项目笔记,基础入门教程)
欢迎转行和学习编程的伙伴,利用更多的资料学习成长比自己琢磨更快哦!
编程学习视频分享:


边栏推荐
- 传奇服务端如何添加地图
- Lenovo Savior r7000+ add ssd+ copy and partition the information of the original D disk to the new SSD
- Network Security Learning - Intranet Security 1
- QT系列---安装
- ARFoundation从零开始9-AR锚点(AR Anchor)
- Let you understand several common traffic exposure schemes in kubernetes cluster
- Jackson parsing JSON detailed tutorial
- 一文带你搞懂环绕通知@Around与最终通知@After的实现
- 2021-10-23
- 小白高薪捷径-Qt开发游戏—贪吃蛇
猜你喜欢

How to add traffic statistics codes to the legendary Development Zone website

What if the computer cannot open excel? The solution of Excel not opening

Solution | get the relevant information about the current employees' highest salary in each department |

手写学生管理系统

What if the office prompts that the system configuration cannot run?

The song of the virtual idol was originally generated in this way!

About realizing page Jump of website in Servlet

js(forEach)出现return无法结束函数的解决方法

【微信小程序--解决display:flex最后一行对齐问题。(不连续排列会分到两边)】

Unity Metaverse(三)、Protobuf & Socket 实现多人在线
随机推荐
The song of the virtual idol was originally generated in this way!
NumPy基础
传奇开区网站如何添加流量统计代码
Mysql多对多关系,分组拼接把多个数据查询到一条数据上
ODOO开发教程之图表
How mongodb inserts, deletes and updates documents
Do you remember the process analysis and function implementation of post notification?
TCP三次握手四次挥手
SM整合原来这么简单,步骤清晰(详细)
三层项目的架构分析及构造方法的参数名称注入
Create a mindscore environment in modelars, install mindvision, and conduct in-depth learning and training (Huawei)
Arfoundation starts from zero 9-ar anchor
QML定制TabBar
What if the computer cannot open excel? The solution of Excel not opening
Quick start JDBC
2022年SPSSPRO认证杯数学建模B题第二阶段方案及赛后总结
What if excel is stuck and not saved? The solution of Excel not saved but stuck
Soft link & hard link
AttributeError: ‘module‘ object has no attribute ‘create_connection‘
【文件下载】Easyexcel快速上手