4419: 【例】指针-3-2
内存限制:128 MB
时间限制:1.000 S
评测方式:文本比较
命题人:
提交:8
解决:8
题目描述
在函数中,通过指针输出数组元素。
样例输入 复制
样例输出 复制
10 20 30
10 20 30
h e l l o
h e l l o
提示
#include <bits/stdc++.h> using namespace std; /* 向函数中传递整数数组,要传递数组的长度因为本质上传递过来的是a[0]的地址 */ void fun1(int a[],int n){ for(int i=0;i< n;i++){ cout<<a[i]<<" "; } cout<<endl; } //向函数中传递数组,直接写成指针的形式 void fun2(int *a,int n){ for(int i=0;i<n;i++){ //cout<<a[i]<<" "; cout<<*a<<" "; a++; } cout<<endl; } //输出字符数组的每个字符 void fun3(char s[]){ for(int i=0;i<strlen(s);i++){ cout<<s[i]<<" "; } cout<<endl; } //采用指针遍历 void fun4(char *s){ while(*s !='\0'){ cout<<*s<<" "; s++; } cout<<endl; } int main(){ int a[3]={10,20,30}; fun1(a,3); fun2(a,3); char s[10]="hello"; fun3(s); fun4(s); return 0; }