一、指针

二、变量与指针

注意区别char 和char *。

!!!!!!!

二、函数与指针

#include<iostream>
#include<string>
using namespace std;
void swap(int *px,int *py)
{
int t;
t=*px;
*px=*py;
*py=t;
};
int main()
{
int x=2,y=3;
cout<<"调用前:x="<<x<<",y="<<y<<endl;
swap(&x,&y);
cout<<"调用后:x="<<x<<",y="<<y<<endl;
}

#include<iostream>
#include<string> using namespace std;
double faver(int a[],int n,int *max,int *min)
{
double aver=0;
for(int i=0;i<n;i++)
{
aver+=a[i];
*max=*min=a[0];
if(*max<a[i])
*max=a[i];
if(*min>a[i])
*min=a[i];
}
return aver/n;
}; int main()
{
int s[]={1,3,5,6,7,8},min,max,n=6;
double aver;
aver=faver(s,n,&max,&min);
cout<<"average="<<aver<<endl;
cout<<"max="<<max<<" min="<<min<<endl; }

注意1、通过指针这种形式,可以将最大和最小值,平均值带回主函数,通过return的话只能带回一个数。2、调用就用&,定义就用*。

#include<iostream>
#include<string> using namespace std; char *noblank(char *str)
{
while(*str=='')
str++;
return str;
}
int main()
{
char *s1=" using namespace std;";
char *s2;
s2=noblank(s1);
cout<<s2<<endl;
return 0; }

觉得这个例子有点扯  

三、数组与指针

#include<iostream>
#include<string> using namespace std; int main()
{
//int a[10]={10,20,30},*p=a,i;
int a[10]={10,20,30},i,*p;
p=a;
cout<<p<<" "<<*p<<endl;
p++;
cout<<p<<" "<<*p<<endl;
}

  

2、指针的关系运算

#include<iostream>
#include<string> using namespace std; int main()
{
double x[5]={1,2,3,4,5.7},*p;
for(p=x;p<x+5;p++)
{
cout<<*p<<" ";
}
cout<<endl;
}

#include<iostream>
#include<string> using namespace std; int main()
{
int a[5]={1,2,3,4,5},i; cout<<"a[i]:";
for(i=0;i<5;i++)
cout<<a[i]<<" ";
cout<<endl; cout<<"*(a+i):";
for(i=0;i<5;i++)
cout<<*(a+i)<<" ";
cout<<endl;
}

可以看到(a)是地址

#include<iostream>
#include<string> using namespace std; int main()
{
int a[5]={1,2,3,4,5},*p=a,i; cout<<"a[i]:";
for(i=0;i<5;i++)
cout<<a[i]<<" ";
cout<<endl; cout<<"*(a+i):";
for(i=0;i<5;i++)
cout<<*(a+i)<<" ";
cout<<endl; cout<<"p[i]:";
for(i=0;i<5;i++)
cout<<p[i]<<" ";
cout<<endl; cout<<"*(p+i):";
for(i=0;i<5;i++)
cout<<*(p+i)<<" ";
cout<<endl;
}

3、二维数组

#include<iostream>
#include<string> using namespace std; int main()
{
int a[2][2]={1,2,4,5},*p;
int max=a[0][0];
for(p=&a[0][0];p<&a[0][0]+4;p++)
{
if(max<*p)
max=*p;
}
cout<<max<<endl; }

#include<iostream>
#include<string> using namespace std; int main()
{
int a[2][3]={1,2,3,4,5,6},(*p)[3];
for(p=a;p<a+2;p++)
{
for(int i=0;i<3;i++)
{
cout<<*(*p+i)<<" ";
}
cout<<endl;
} }

例子

#include<iostream>
#include<string> using namespace std; char *strchr(char *str,char c)
{
while(*str!='\0')
{
if(*str==c)
return str;
str++;
}
return NULL;
}
int main()
{
char *str="abcdefghij";
char *p;
p=strchr(str,'a');
if(p==NULL)
cout<<"Null";
else
cout<<p-str<<endl; }

#include<iostream>
#include<string> using namespace std; char *strchr(char *str,char c)
{
while(*str!='\0')
{
if(*str==c)
return str;
str++;
}
return NULL;
}
int main()
{
char str[]="abcdefghij";
char *p;
p=strchr(str,'c');
if(p==NULL)
cout<<"Null";
else
cout<<p-str<<endl; }

5、指针与结构体

6、动态数组

#include<iostream>
#include<string> using namespace std; int main()
{
int n,*p;
cout<<"请输入n值";
cin>>n;
p=new int[n];
if(p==NULL)
{
cout<<"空间申请失败";
}
for(int i=0;i<n;i++)
cin>>p[i];
for(int j=0;j<n;j++)
cout<<p[j]<<" "<<endl;
return 0; }

  

  

  

  

  

  

  

  

  

  

C++ 按址操作的更多相关文章

  1. RTTI、虚函数和虚基类的实现方式、开销分析及使用指导(虚函数的开销很小,就2次操作而已)

    白杨 http://baiy.cn “在正确的场合使用恰当的特性” 对称职的C++程序员来说是一个基本标准.想要做到这点,首先要了解语言中每个特性的实现方式及其开销.本文主要讨论相对于传统 C 而言, ...

  2. C语言 结构体传值与传址分析

    /********************************************************************** * 版权所有 (C)2017, Wang maochun ...

  3. 编译器开发系列--Ocelot语言5.表达式的有效性检查

    本篇将对"1=3""&5"这样无法求值的不正确的表达式进行检查. 将检查如下这些问题.●为无法赋值的表达式赋值(例:1 = 2 + 2)●使用非法的函数 ...

  4. C++ 指向成员函数指针问题

    成员函数指针与常规指针不同,一个指向成员变量的指针并不指向一个内存位置.通常最清晰的做法是将指向数据成员的指针看作为一个偏移量. class ru_m { public: typedef int (r ...

  5. char* 和char[]的区别

    以下内容均来自互联网,系笔者汇总并总结. 1. 问题介绍 问题引入:在实习过程中发现了一个以前一直默认的错误,同样char *c = "abc"和char c[]="ab ...

  6. 【C】 04 - 表达式和语句

    程序的生命力体现在它千变万化的行为,而再复杂的系统都是由最基本的语句组成的.C语句形式简单自由,但功能强大.从规范的角度学习C语法,一切显得简单而透彻,无需困扰于各种奇怪的语法. 1. 表达式(exp ...

  7. C++ 虚函数在基类与派生类对象间的表现及其分析

    近来看了侯捷的<深入浅出MFC>,读到C++重要性质中的虚函数与多态那部分内容时,顿时有了疑惑.因为书中说了这么一句:使用“基类之指针”指向“派生类之对象”,由该指针只能调用基类所定义的函 ...

  8. drupal module 自定义

    01 <?php function mytracer_menu() { $items = array(); $items['admin/config/mytracer'] = array( 't ...

  9. typeof、offsetof、container_of的解释

    链表是内核最经典的数据结构之一,说到链表就不得不提及内核最经典(没有之一)的宏container_of. container_of似乎就是为链表而生的,它的主要作用是根据一个结构体变量中的一个域成员变 ...

随机推荐

  1. LeetCode37 使用回溯算法实现解数独,详解剪枝优化

    本文始发于个人公众号:TechFlow,原创不易,求个关注 数独是一个老少咸宜的益智游戏,一直有很多拥趸.但是有没有想过,数独游戏是怎么创造出来的呢?当然我们可以每一关都人工设置,但是显然这工作量非常 ...

  2. 峰哥说技术:07-SpringBoot 正好Thymeleaf视图

    Spring Boot深度课程系列 峰哥说技术—2020庚子年重磅推出.战胜病毒.我们在行动 07  峰哥说技术:SpringBoot 正好Thymeleaf视图 Spring Boot视图介绍 虽然 ...

  3. VUE一 基础语法

    一.模板语法 二.Class和Style绑定 三.条件渲染 四.vue事件处理器 五.vue组件

  4. HDFS 客户端读写操作详情

    1. 写操作 客户端向namenode发起上传请求 namenode检查datanode是否已经存有该文件,并且检查客户端的权限 确认可以上传后,根据文件块数返回datanode栈 注:namenod ...

  5. IRM3800 红外遥控器解码 linux驱动

    这一次还是接在 Cemera 上.用 中断引脚 EINT20 也就是 GPG12. 之前焊的 51 板子上有一个红外接收器. 请注意了,是 标准的 NEC 码规范:首次发送的是9ms的高电平脉冲,其后 ...

  6. JAVA校内赛

    第一题: 问题描述 在计算机存储中,15.125GB是多少MB?答案提交 这是一道结果填空的题,你只需要算出结果后提交即可.本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分. ...

  7. Excel之在单元格中生成随机密码

    公式 =CHAR(INT(RAND()*26+97))&INT(RAND()*10)&CHAR(INT(RAND()*26+97))&INT(RAND()*10) 分析 CHA ...

  8. 7,MapReduce基础

    目录 MapReduce基础 一.关于MapReduce 二.MapReduce的优缺点 三.MapReduce的执行流程 四.编写MapReduce程序 五.MapReduce的主要执行流程 Map ...

  9. flask 分页数据显示

    填充一些数据在表中 @blue.route('/pages/') def pages(): # 默认进入这个视图函数 第一页并只显示5条数据 page = request.args.get('page ...

  10. 处理asp.net core连接mysql的一个异常Sequence contains more than one matching element

    晚上在那里调程序,把mysql.data组件的nuget包进行了更新,前几天好好的程序,开始抛错,跟踪断点发现以下的异常: Unable to connect to any of the specif ...