• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C语言 > C++编程中指针的声明与基本使用讲解

C++编程中指针的声明与基本使用讲解

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-28

通过本文主要向大家介绍了c++编程,c++编程软件,c++编程软件下载,c++编程思想,c++游戏编程等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

使用以下序列声明指针。

[storage-class-specifiers] [cv-qualifiers] type-specifiers 
[ms-modifier] declarator ;
</div>

其中,任何有效指针声明符均可用于 declarator。简单指针声明符的语法如下所示:

* [cv-qualifiers] identifier [= expression]
</div>

1.声明说明符:
可选存储类说明符。
应用于要指向的对象的类型的可选 const 或 volatile 关键字。
类型说明符:可表示要指向的对象的类型的类型名称。
2.声明符:
可选的 Microsoft 专用修饰符。

* 运算符。
应用于指针本身的可选 const 或 volatile 关键字。
标识符。
可选初始值设定项。
指向函数的指针的声明符类似于以下形式:

(* [cv-qualifiers] identifier )( argument-list ) [cv-qualifers]
[exception specification] [= expression];
</div>

对于指针数组,语法如下所示:

* identifier [ [ constant-expression ] ]
</div>

但是,指针声明符可能更复杂。
多个声明符及其初始值设定项可能同时出现在前面有声明说明符且以逗号分隔的列表中的一个声明中。
指针声明的简单示例如下:

char *pch;
</div>

前面的声明指定 pch 指向 char 类型的对象。
更复杂的示例是

static unsigned int * const ptr;
</div>

前面的声明指定 ptr 是一个指向 unsigned int 类型(带静态存储持续时间)的对象的常量指针。
下一个示例演示如何声明和初始化多个指针:

static int *p = &i, *q = &j;
</div>

在前面的示例中,指针 p 和 q 都指向类型 int 的对象并分别初始化为 i 和 j 的地址。存储类说明符 static 应用于这两个指针。

// pointer.cpp
// compile with: /EHsc
#include <iostream>
int main() {
 int i = 1, j = 2; // local variables on the stack
 int *p;

 // a pointer may be assigned to "point to" the value of
 // another variable using the & (address of) operator
 p = & j; 

 // since j was on the stack, this address will be somewhere
 // on the stack. Pointers are printed in hex format using
 // %p and conventionally marked with 0x. 
 printf_s("0x%p\n", p);

 // The * (indirection operator) can be read as "the value
 // pointed to by".
 // Since p is pointing to j, this should print "2"
 printf_s("0x%p %d\n", p, *p);

 // changing j will change the result of the indirection
 // operator on p.
 j = 7;
 printf_s("0x%p %d\n", p, *p );

 // The value of j can also be changed through the pointer
 // by making an assignment to the dereferenced pointer
 *p = 10;
 printf_s("j is %d\n", j); // j is now 10

 // allocate memory on the heap for an integer,
 // initialize to 5
 p = new int(5);

 // print the pointer and the object pointed to
 // the address will be somewhere on the heap
 printf_s("0x%p %d\n", p, *p);

 // free the memory pointed to by p
 delete p;

 // At this point, dereferencing p with *p would trigger
 // a runtime access violation.

 // Pointer arithmetic may be done with an array declared
 // on the stack or allocated on the heap with new.
 // The increment operator takes into account the size 
 // of the objects pointed to.
 p = new int[5];
 for (i = 0; i < 5; i++, p++) {
 *p = i * 10;
 printf_s("0x%p %d\n", p, *p);
 }

 // A common expression seen is dereferencing in combination
 // with increment or decrement operators, as shown here.
 // The indirection operator * takes precedence over the 
 // increment operator ++. 
 // These are particularly useful in manipulating char arrays.
 char s1[4] = "cat";
 char s2[4] = "dog";
 char* p1 = s1;
 char* p2 = s2;

 // the following is a string copy operation
 while (*p1++ = *p2++);

 // s2 was copied into s1, so now they are both equal to "dog"
 printf_s("%s %s", s1, s2);
}

</div>

输出:

0x0012FEC8
0x0012FEC8 2
0x0012FEC8 7
j is 10
0x00320850 5
0x00320850 0
0x00320854 10
0x00320858 20
0x0032085C 30
0x00320860 40
dog dog
</div>

另一个示例演示如何在数据结构中使用指针;本例中采用链接列表。

// pointer_linkedlist.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;

struct NewNode {
 NewNode() : node(0){}
 int i;
 NewNode * node;
};

void WalkList(NewNode * ptr) {
 if (ptr != 0) {
 int i = 1;
 while (ptr->node != 0 ) {
  cout << "node " << i++ << " = " << ptr->i << endl;
  ptr = ptr->node;
 }
 cout << "node " << i++ << " = " << ptr->i << endl;
 }
}

void AddNode(NewNode ** ptr) {
 NewNode * walker = 0;
 NewNode * MyNewNode = new NewNode;
 cout << "enter a number: " << endl;
 cin >> MyNewNode->i;

 if (*ptr == 0)
 *ptr = MyNewNode;
 else {
 walker = *ptr;
 while (walker->node != 0)
  walker = walker->node;

 walker->node = MyNewNode;
 }
}

int main() {
 char ans = ' ';
 NewNode * ptr = 0;
 do {
 cout << "a (add node) d (display list) q (quit)" << endl;
 cin >> ans;
 switch (ans) {
 case 'a':
  AddNode(&ptr);
  break;
 case 'd':
  WalkList(ptr);
  break;
 }
 } while (ans != 'q');
}

</div>

输出:

a
45
d
a
789
d
qa (add node) d (display list) q (quit)
enter a number:
a (add node) d (display list) q (quit)
node 1 = 45
a (add node) d (display list) q (quit)
enter a number:
a (add node) d (display list) q (quit)
node 1 = 45
node 2 = 789
a (add node) d (display list) q (quit)
</div>

固定和可变指针

const 和 volatile 关键字用于更改处理指针的方式。 const 关键字指定指针在初始化后无法修改;此后指针将受到保护,防止进行修改。
volatile 关键字指定与后跟的名称关联的值可由用户应用程序中的操作以外的操作修改。因此,volatile 关键字对于声明共享内存中可由多个进程访问的对象或用于与中断服务例程通信的全局数据区域很有用。
如果某个名称被声明为 volatile,则每当程序访问该名称时,编译器都会重新加载内存中的值。这将显著减少可能的优化。但是,当对象的状态可能意外更改时,这是保证可预见的程序性能的唯一方法。
若要将指针指向的对象声明为 const 或 volatile,请使用以下形式的声明:

const char *cpch;
volatile char *vpch;
</div>

若要将指针的值(即指针中存储的实际地址)声明为 const 或 volatile,请使用以下形式的声明:

char * const pchc;
char * volatile pchv;
</div>

C++ 语言会阻止将允许修改声明为 const 的对象或指针的赋值。此类赋值会移除用来声明对象或指针的信息,从而违反原始声明的意图。请考虑以下声明:

const char cch = 'A';
char ch = 'B';
</div>

假定前面声明了两个对象(const char 类型的 cch 和 char 类型的 ch),以下声明/初始化将是有效的:

const char *pch1 = &cch;
const char *const pch4 = &cch;
const char *pch5 = &ch;
char *pch6 = &ch;
char *const pch7 = &ch;
const char *const pch8 = &ch;
</div>

以下声明/初始化存在错误。

char *pch2 = &cch;  // Error
char *const pch3 = &cch;  // Error
</div>

pch2 的声明声明了一个可以用来修改常量对

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C++的虚析构详解及实例代码
  • C++二分查找(折半查找)算法实例详解
  • C++ 中函数重载、覆盖与隐藏详解
  • 学习C++编程的必备软件
  • C++调用Python基础功能实例详解
  • C++中函数重载实例详解
  • C++模版函数详解
  • 详解C++11中的右值引用与移动语义
  • C++详解默认参数的构造函数及简单实例代码
  • C++如何动态的生成对象详解

相关文章

  • 2017-05-28怎么通过C语言自动生成MAC地址
  • 2017-05-28C语言 实现N阶乘的程序代码
  • 2017-05-28C++面向对象实现五子棋小游戏
  • 2017-05-28关于"引用"的几点说明介绍
  • 2017-05-28c++中for双循环的那些事
  • 2017-05-28c++ 指针与引用的区别介绍及使用说明
  • 2017-05-28从汇编看c++中多态的应用
  • 2017-05-28Mygui中文换行问题解决方案
  • 2017-05-28使用C语言解决字符串全排列问题
  • 2017-05-28详解C语言中fseek函数和ftell函数的使用方法

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • C语言fscanf和fprintf函数的用法详解(格式化读写文件)
    • C++函数的嵌套调用和递归调用学习教程
    • C++学习小结之数据类型及转换方式
    • C++编程中使用设计模式中的policy策略模式的实例讲解
    • 基于C++全局变量的声明与定义的详解
    • C++中输入输出流及文件流操作总结
    • c语言socket多线程编程限制客户端连接数
    • C++ COM编程之QueryInterface函数(一)
    • c语言调用汇编的方法
    • C语言中对文件最基本的读取和写入函数

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有