当前位置:网站首页>指针与数组在函数中输入实现逆序输出
指针与数组在函数中输入实现逆序输出
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;
}
输出结果
边栏推荐
- 史上最全学习率调整策略lr_scheduler
- 英特尔与信步科技共同打造机器视觉开发套件,协力推动工业智能化转型
- Introduction to the PureMVC series
- 树与图的深度优先遍历模版原理
- 案例大赏:英特尔携众多合作伙伴推动多领域AI产业创新发展
- Comment les tests de logiciels sont - ils effectués sur le site Web? Testez la stratégie!
- sscanf,sscanf_ S and its related usage "suggested collection"
- Camera calibration (I): robot hand eye calibration
- Field data acquisition and edge calculation scheme of CNC machine tools
- Chapter 9 Yunji datacanvas was rated as 36 krypton "the hard core technology enterprise most concerned by investors"
猜你喜欢
【实践出真理】import和require的引入方式真的和网上说的一样吗
案例大赏:英特尔携众多合作伙伴推动多领域AI产业创新发展
Camera calibration (I): robot hand eye calibration
Analyse approfondie de kubebuilder
Chapter 9 Yunji datacanvas company won the highest honor of the "fifth digital finance innovation competition"!
【736. Lisp 语法解析】
九章云极DataCanvas公司获评36氪「最受投资人关注的硬核科技企业」
Lessons and thoughts of the first SQL injection
深耕开发者生态,加速AI产业创新发展 英特尔携众多合作伙伴共聚
AI landing new question type RPA + AI =?
随机推荐
acwing 843. N-queen problem
How to open win11 remote desktop connection? Five methods of win11 Remote Desktop Connection
A picture to understand! Why did the school teach you coding but still not
掌握软件安全测试方法秘笈,安全测试报告信手捏来
The easycvr platform is connected to the RTMP protocol, and the interface call prompts how to solve the error of obtaining video recording?
Advertising attribution: how to measure the value of buying volume?
This "advanced" technology design 15 years ago makes CPU shine in AI reasoning
A series of shortcut keys for jetbrain pychar
Organize five stages of actual attack and defense drill
广告归因:买量如何做价值衡量?
抖音或将推出独立种草社区平台:会不会成为第二个小红书
深耕开发者生态,加速AI产业创新发展 英特尔携众多合作伙伴共聚
Fix the problem that the highlight effect of the main menu disappears when the easycvr Video Square is clicked and played
Kivy tutorial of setting the size and background of the form (tutorial includes source code)
Factor analysis r practice (with R installation tutorial and code)
Windows are not cheap things
How to conduct website testing of software testing? Test strategy let's go!
R语言主成分pca、因子分析、聚类对地区经济研究分析重庆市经济指标
【数模】Matlab allcycles()函数的源代码(2021a之前版本没有)
GPT-3当一作自己研究自己,已投稿,在线蹲一个同行评议