一、指针

二、变量与指针

注意区别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. removeAttribute getAttribute setAttribute

    1.removeAttribute() 方法删除指定的属性. 注:removeAttributeNode() 方法从元素中删除指定的属性节点.简单的来讲,removeAttribute() 移除元素内 ...

  2. CentOS下安装Anaconda和pycharm

    前情提要:Linux越来越受大家喜爱,而在Linux中有一个社区很活跃的系统:那就是CentOS:而Anaconda又是几乎就一劳永逸的,你装了它之后基本上很多类库就不用再装了.然后就是pycharm ...

  3. 基于springboot多模块项目使用maven命令打成war包放到服务器上运行的问题

    首先,大家看到这个问题,可能并不陌生,而且脑子里第一映像就是使用mava中的clear package 或者 clear install进行打包,然后在项目中的target文件夹下面找到xxx.war ...

  4. Spring Boot 2.x基础教程:使用MyBatis的XML配置方式

    上一篇我们介绍了如何在Spring Boot中整合我们国人最常用的MyBatis来实现对关系型数据库的访问.但是上一篇中使用了注解方式来实现,而对于很多MyBatis老用户还是习惯于XML的开发方式, ...

  5. git add的各种情况分类

    ·  git add -A  提交所有变化 ·  git add -u  提交被修改(modified)和被删除(deleted)文件,不包括新文件(new) ·  git add .  提交新文件( ...

  6. ios background task

    今天要实现一个需求,当用户触摸HOME键,将应用切换到后台时,启动自动备份的任务.这涉及到ios的后台任务处理,本文简单总结一下 首先,ios app有5种状态,分别是:not running, in ...

  7. 基于正向扫描的并行区间连接平面扫描算法(IEEE论文)

    作者: Panagiotis Bouros ∗Department of Computer ScienceAarhus University, Denmarkpbour@cs.au.dkNikos M ...

  8. Scrapy 入门教程

    Scrapy 是用 Python 实现的一个为了爬取网站数据.提取结构性数据而编写的应用框架. Scrapy 常应用在包括数据挖掘,信息处理或存储历史数据等一系列的程序中. 通常我们可以很简单的通过 ...

  9. C++ 指针偏移的理解

    //题目:若有程序段int a[5] = { 1, 2, 3, 4, 5 }; int *p = (int *)(&a + 1); printf("%d,%d", *(a ...

  10. css 实现九宫格

    1.自己写了一个,写完对比了下别人写的发现自己写的太low.故就不写自己太差劲的了. 别人写的我总结优化了一下,如果不用写内容去掉position,content简单也是可以的. <!DOCTY ...