当前位置:网站首页>指针与数组在函数中输入实现逆序输出
指针与数组在函数中输入实现逆序输出
2022-07-06 22:21:00 【小浩编程】
输入什么输出什么
#include <stdio.h>
void initarray(int *parr,int size)
{
int i;
for(i=0;i<size;i++)
{
printf("请输入第%d个元素的数据:\n",i+1);
scanf("%d",parr++);//parr就是地址了(指针),就不用再取地址了
}
}
void printarray(int *parr,int size)
{
int i;
for(i=0;i<size;i++)
{
printf("%d ",*parr++);//对地址(指针)取值,打出来
}
}
int main()
{
int arry[5];
int size = sizeof(arry)/sizeof(arry[0]);
initarray(arry,size);//arry 数组名就是数组地址、首元素地址
printarray(&arry[0],size);//&arry[0]是取首元素地址与上面相同
return 0;
}
输出结果
逆序输入输出
#include <stdio.h>
void initarray(int *parr,int size)
{
int i;
for(i=0;i<size;i++)
{
printf("请输入第%d个元素的数据:\n",i+1);
scanf("%d",parr++);//parr就是地址了(指针),就不用再取地址了
}
}
/* void revangearry(int *parr,int size)//将输入的数据进行逆序输出 { int i,j; int tmp; for(i=0;i<size/2;i++) { j = size-1-i; tmp = parr[i]; parr[i] = parr[j]; parr[j] = tmp; } } */
//上面函数换种写法,更贴近与指针的写法
void revangearry(int *parr,int size)//将输入的数据进行逆序输出
{
int i,j;
int tmp;
for(i=0;i<size/2;i++)
{
j = size-1-i;
tmp = *(parr+i);//数组地址按照数组类型直接偏移,也是得到了与上面同等效果
*(parr+i) = *(parr+j);//按照数组类型进行地址偏移,在相应地址进行更改了数值
*(parr+j) = tmp;
}
}
void printarray(int *parr,int size)
{
int i;
for(i=0;i<size;i++)
{
printf("%d ",*parr++);//对地址(指针)取值,打出来
}
}
int main()
{
int arry[5];
int size = sizeof(arry)/sizeof(arry[0]);
initarray(arry,size);//arry 数组名就是数组地址、首元素地址
revangearry(arry,size);//逆序处理
printarray(&arry[0],size);//&arry[0]是取首元素地址与上面相同
return 0;
}
输出结果
边栏推荐
- SQL where multiple field filtering
- The request request is encapsulated in uni app, which is easy to understand
- 5G VoNR+之IMS Data Channel概念
- Break the memory wall with CPU scheme? Learn from PayPal to expand the capacity of aoteng, and the volume of missed fraud transactions can be reduced to 1/30
- Comment les tests de logiciels sont - ils effectués sur le site Web? Testez la stratégie!
- 论文上岸攻略 | 如何快速入门学术论文写作
- Read of shell internal value command
- Implementation of JSTL custom function library
- Organize five stages of actual attack and defense drill
- 【线段树实战】最近的请求次数 + 区域和检索 - 数组可修改+我的日程安排表Ⅰ/Ⅲ
猜你喜欢
A line of R code draws the population pyramid
mpf2_线性规划_CAPM_sharpe_Arbitrage Pricin_Inversion Gauss Jordan_Statsmodel_Pulp_pLU_Cholesky_QR_Jacobi
Section 1: (3) logic chip process substrate selection
窗口可不是什么便宜的东西
Fix the problem that the highlight effect of the main menu disappears when the easycvr Video Square is clicked and played
深耕开发者生态,加速AI产业创新发展 英特尔携众多合作伙伴共聚
JDBC link Oracle reference code
Read of shell internal value command
[line segment tree practice] recent requests + area and retrieval - array modifiable + my schedule I / III
What if the win11 screenshot key cannot be used? Solution to the failure of win11 screenshot key
随机推荐
程序员上班摸鱼,这么玩才高端!
What is JVM? What are the purposes of JVM tuning?
Depth first traversal template principle of tree and graph
Fix the problem that the highlight effect of the main menu disappears when the easycvr Video Square is clicked and played
How to conduct website testing of software testing? Test strategy let's go!
九章云极DataCanvas公司摘获「第五届数字金融创新大赛」最高荣誉!
Common methods of list and map
Two methods of chromosome coordinate sequencing
ESG全球领导者峰会|英特尔王锐:以科技之力应对全球气候挑战
未婚夫捐5亿美元给女PI,让她不用申请项目,招150位科学家,安心做科研!
软件测试之网站测试如何进行?测试小攻略走起!
Have you got the same "artifact" of cross architecture development praised by various industry leaders?
[line segment tree practice] recent requests + area and retrieval - array modifiable + my schedule I / III
Master the secrets of software security testing methods, and pinch the security test report with your hands
[hand torn STL] list
Section 1: (3) logic chip process substrate selection
微信能开小号了,拼多多“砍一刀”被判侵权,字节VR设备出货量全球第二,今日更多大新闻在此
Lecture 3 of "prime mover x cloud native positive sounding, cost reduction and efficiency enhancement lecture" - kubernetes cluster utilization improvement practice
每人每年最高500万经费!选人不选项目,专注基础科研,科学家主导腾讯出资的「新基石」启动申报
Local tool [Navicat] connects to remote [MySQL] operation