当前位置: 首页 > news >正文

网站宽度设置昆明网络推广哪家好

网站宽度设置,昆明网络推广哪家好,网站开发工程师是什么内容,网站开发空间小目录 叁、函数与字符串 肆、函数与指针 4.1 指针作为函数参数 4.2 函数返回指针 4.3 函数指针与函数指针数组 4.4 结构体指针 ​​​​​​​​​​​​​​小樽C 多章⑧ (壹) 指针变量https://blog.csdn.net/weixin_44775255/article/details/129031168 小樽C 多章⑧ …

目录

叁、函数与字符串

肆、函数与指针

4.1 指针作为函数参数

4.2 函数返回指针

4.3 函数指针与函数指针数组

4.4 结构体指针 


​​​​​​​​​​​​​​小樽C++ 多章⑧ (壹) 指针变量icon-default.png?t=N176https://blog.csdn.net/weixin_44775255/article/details/129031168

小樽C++ 多章⑧ (贰) 指针与数组icon-default.png?t=N176https://blog.csdn.net/weixin_44775255/article/details/129396791

叁、函数与字符串

说道字符串,我们要导入字符串库#include<cstring>

学会字符串的常用方法。strcpy,strcmp,strstr,strlen。

//字符串 
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char a[100],b[100]; 
int main(){strcpy(a,"hello");
//	cout<<a<<endl;printf("%s,len=%d\n",a,strlen(a)); //字符串长度 scanf("%s",b);int cmp = strcmp(a,b); //字符串比较大小 if(cmp==0){printf("%s=%s\n",a,b);}else if(cmp<0){printf("%s<%s\n",a,b);}else{printf("%s>%s\n",a,b);}if(strstr(a,b)!= NULL){ 	//查找子串 printf("%s in %s\n",b,a); }return 0;
} 

难点:具体来看看strcpy、strlen的来历

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char *strcpy(char *dest,const char *scr){char *p=dest;while(*scr != '\0'){*dest = *scr;dest++;scr++;}*dest = '\0';return p;
} 
int main(){char *a=new char;*strcpy(a,"cvbnm");cout<<a<<endl;*strcpy(a,"asd");cout<<a;return 0;
} 

strcpy 是 赋值的含义,把某个已知的字符串值赋值到a变量,输出a变量就有值了。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
size_t strlen(const char *str){const char *cp = str;while (*cp++){;}return (cp-str-1);
}
int main(){cout<<strlen("abcdr")<<endl;
}

 

 这些函数的实现都是指针操作!虽然也可以用数组来实现,但数组的存储效率会低一些。


肆、函数与指针

4.1 指针作为函数参数

自定义函数的参数可以有整型、浮点型,那可不可以用指针类型?

那来试试用指针参数实现交换两个变量的值。

例子1.交换两个值,再比比大小。

#include<iostream>
#include<cstdio> 
using namespace std;
void swap(int *x,int *y){ //交换两个值 int t = *x;*x = *y;*y = t;
}
void sort(int *x,int *y,int *z){ if(*x > *y) swap(x,y); //比大小 if(*x > *z) swap(x,z);if(*y > *z) swap(y,z);
} 
int main(){int a,b,c;scanf("%d%d%d",&a,&b,&c);sort(&a,&b,&c);printf("a=%d,b=%d,c=%d",a,b,c);return 0;
}


4.2 函数返回指针

用指针作为函数, 例如:int *a(int a,int b)。

炼1.实现一个包含n个整数的数组中找到第一个质数,若有则返回函数的地址,没则返回NULL。

#include<iostream>
#include<cstdio> 
#include<cmath>
using namespace std;
int n,a[1000];
bool isprime(int n){//求质数 if (n<2) return false;if(n==2) return true;for(int i=2;i<=sqrt(n);i++){if(n%i==0){return false;}}	return true;
} int *find(){ //指针查找函数 for(int i=1;i<=n;i++){if(isprime(a[i])){return &a[i];}}return NULL;
}
int main(){cin>>n;for(int i=1;i<=n;i++){cin>>a[i];}int *p = find();if(p != NULL){cout<<p<<" "<<*p;}return 0;
}


4.3 函数指针与函数指针数组

(1) 一般定义函数: int test(int );  那换成指针函数 int (*test)( int ); 

注意不可以写成: int *test ( int ); 这样子编程会 声明定义成 test ( int )的函数,返回类型是 int * 。

(2) 获取函数的地址。 跟数组一样,函数名就是地址,函数名也可看成是指针。

1.用 typedef 声明函数指针类型

#include<iostream>
#include<cstdio> 
using namespace std;
//函数指针 
int sum(int a,int b){return a+b;
} 
typedef int (*LP)(int,int); 
//定义声明了LP类型的函数指针,内有2个参数。
int main(){LP p = sum; // 定义LP类型的指针p cout<<p(2,5); //函数指针p调用参数return 0;
}

  

 2.模拟菜单功能实现例子,函数指针数组

//函数指针数组 
void t1(){	cout<<"test1"<<endl; } 
void t2(){	cout<<"test2"<<endl; } 
void t3(){	cout<<"test3"<<endl; } 
void t4(){	cout<<"test4"<<endl; } 
typedef void(*Tp)();
定义声明了空类型的函数指针 Tp,无参数。
int main(){Tp a[] = {t1,t2,t3,t4}; 定义TP类型的函数指针数组a int x;cin>>x;a[x](); return 0;
}


4.4 结构体指针 

  1. 定义一个结构体,有名字、性别、成绩成员。
  2. 声明结构体并赋值,还声明结构体指针;
#include<iostream>
#include<cstdio> 
using namespace std;
struct Student{char name[20];char sex;float score;
};Student *p;
Student stu={"lisi",'m',95.5};
  1. 3.指针访问结构体成员方法:
  • (*指针名).成员名  (*p).name
  • 指针名->成员名  p->name
int main(){cout<<stu.name<<endl;p = &stu; //指针指向stu地址 cout<<(*p).name<<endl;
//	(*p).name 效果等同 stu.namecout<<(*p).sex<<endl;cout<<p->score; 
//	p->score 效果等同(*p).scorereturn 0;
} 

 由于一般引用结构体变量,通常要对整个的结构体进行引用,效率不高;所以一般用指针会提高写效率。

http://www.yayakq.cn/news/668282/

相关文章:

  • 临沂 网站优化网上做一道题2元的网站
  • 寻花问柳-专注做一家男人的网站猪软件行业发展趋势
  • 沧州网站建设专业定制加强制度建设 信息公开 网站 专栏
  • 网站gif素材低代码前端开发平台
  • 外包网站设计百度有哪些网站可免费做软件推广
  • 高端网站开发哪家强抖音流量推广神器软件免费
  • 自适应网站是什么建站之星有手机版模板
  • 企业建网站哪家好网站建设策划文案
  • 搭建网站要多久湛江设计公司
  • 商城站到汤泉池无锡做网站费用
  • 网站怎么才可以不备案苏州建网站的公司平台收费标准
  • 房山 网站建设百度搜索广告推广
  • 网站网格河南有名的做网站公司
  • 免费的网站软件正能量咨询网站建设
  • 建立网站程序北京网站建设哪家最好
  • 大兴安岭做网站管理培训网站建设
  • 门户网站建设投资网站建设合伙人
  • 恩施网站建设公司wordpress域名变回ip
  • 做的好的奥运会网站精品资源共享课程网站建设论文
  • 西安市城市建设管理局网站网站开发名片
  • wordpress网站不显示系列网站 业务范围
  • 3免费做网站重庆建设厂招聘信息网站
  • 昆山做网站公司个人简历模板大全
  • 高端企业网站建设的核心是什么帝国网站管理系统安装教程
  • 如何添加网站关键词h5制作网页
  • 网站建设服务收费标准青岛网络营销网络推广介绍
  • 这些网站涵盖了不同的主题和类型长春免费网站制作
  • 网站建设外文版政策文件做财经直播网站
  • 免费word模板网站耒阳网站建设
  • 海南省建设考试网站贵州建设厅考试网站二建成绩查询