当前位置:网站首页>指针与数组在函数中输入实现逆序输出
指针与数组在函数中输入实现逆序输出
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;
}
输出结果
边栏推荐
- R语言主成分pca、因子分析、聚类对地区经济研究分析重庆市经济指标
- 什么是Web3
- 【实践出真理】import和require的引入方式真的和网上说的一样吗
- 微信能开小号了,拼多多“砍一刀”被判侵权,字节VR设备出货量全球第二,今日更多大新闻在此
- 关于01背包个人的一些理解
- Intel David tuhy: the reason for the success of Intel aoten Technology
- Two methods of chromosome coordinate sequencing
- JS form get form & get form elements
- Both primary and secondary equipment numbers are 0
- Mathematical analysis_ Notes_ Chapter 10: integral with parameters
猜你喜欢

Network Security Learning - Information Collection

Vscode automatically adds a semicolon and jumps to the next line

窗口可不是什么便宜的东西
![Local tool [Navicat] connects to remote [MySQL] operation](/img/e8/a7533bac4a70ab5aa3fe15f9b0fcb0.jpg)
Local tool [Navicat] connects to remote [MySQL] operation

In depth analysis of kubebuilder

Analyse approfondie de kubebuilder

3GPP信道模型路损基础知识

Mathematical analysis_ Notes_ Chapter 10: integral with parameters

AI 落地新题型 RPA + AI =?

acwing 843. N-queen problem
随机推荐
Common methods of list and map
jvm是什么?jvm调优有哪些目的?
System framework of PureMVC
leetcode 53. Maximum subarray maximum subarray sum (medium)
Data security -- 12 -- Analysis of privacy protection
英特尔与信步科技共同打造机器视觉开发套件,协力推动工业智能化转型
Detect when a tab bar item is pressed
The worse the AI performance, the higher the bonus? Doctor of New York University offered a reward for the task of making the big model perform poorly
两个div在同一行,两个div不换行「建议收藏」
MySQL split method usage
【數模】Matlab allcycles()函數的源代碼(2021a之前版本沒有)
架构实战训练营|课后作业|模块 6
程序员上班摸鱼,这么玩才高端!
深入解析Kubebuilder
【数模】Matlab allcycles()函数的源代码(2021a之前版本没有)
全国气象数据/降雨量分布数据/太阳辐射数据/NPP净初级生产力数据/植被覆盖度数据
Gpt-3 is a peer review online when it has been submitted for its own research
Camera calibration (I): robot hand eye calibration
acwing 843. n-皇后问题
Wechat can play the trumpet. Pinduoduo was found guilty of infringement. The shipment of byte VR equipment ranks second in the world. Today, more big news is here