一、指针

二、变量与指针

注意区别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. MVC06

    1.校验机制 我们可以在Model中使用属性进行校验 using System; using System.ComponentModel.DataAnnotations; using System.D ...

  2. 从HTML标签开始

    开始这一切吧! 没错,你没看错,我将从HTML标签开始我的整个系列文章.很基础吧?但是每个前端人都是从最简单的HTML标签开始的,都是从一个<html></html>开始整个前 ...

  3. Codeforces Round #295 (Div. 2) B. Two Buttons 520B

    B. Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  4. SpringBoot入门系列(四)整合模板引擎Thymeleaf

    前面介绍了Spring Boot的优点,然后介绍了如何快速创建Spring Boot 项目.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/ ...

  5. JavaScript实现树结构(二)

    JavaScript实现树结构(二) 一.二叉搜索树的封装 二叉树搜索树的基本属性: 如图所示:二叉搜索树有四个最基本的属性:指向节点的根(root),节点中的键(key).左指针(right).右指 ...

  6. 机器学习实战:意大利Covid-19病毒感染数学模型及预测

    作者:Gianluca Malato deephub翻译组:刘欣然 当今世界正在与一个新的敌人作斗争,那就是Covid-19病毒. 该病毒自首次在中国出现以来,在世界范围内迅速传播.不幸的是,意大利的 ...

  7. 实验一 Linux系统与应用准备(嵌入式Linux工程师的“修真之路”)

    作业格式 项目 内容 这个作业属于哪个课程 这里是链接[https://edu.cnblogs.com/campus/nchu/2020SpringSystemAndApplication] 这个作业 ...

  8. si4745 FM-AM-SW 音量控制芯片 驱动详解

    在论坛上看到有人发这个dsp 芯片,仔细看了下,发现功能正合我意,网上能找到的资料(源码)不多 软件环境:linux4.1.36  arm-linux-gcc 4.3.2 实现功能:自动搜台,上一台, ...

  9. BeetleX.FastHttpApi之测试插件集成

    说到Webapi测试工具相信很多人想起Swagger,它可以非常方便地集成到项目中并进行项目Webapi接口测试.而BeetleX.FastHttpApi在新版本中也提供类似的插件,只需要引用这个插件 ...

  10. 前端要了解的seo

    一.搜索引擎工作原理 当我们在输入框中输入关键词,点击搜索或查询时,然后得到结果.深究其背后的故事,搜索引擎做了很多事情. 在搜索引擎网站,比如百度,在其后台有一个非常庞大的数据库,里面存储了海量的关 ...