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

2020-01-06 14:31:32丽君
易采站长站为您分析详解C++编程中C++编程中指针的声明与基本使用讲解,文中举了简单的例子来讲如何在基本的数据结构中使用指针,以及固定和可变指针的介绍,需要的朋友可以参考下  

使用以下序列声明指针。


[storage-class-specifiers] [cv-qualifiers] type-specifiers 
[ms-modifier] declarator ;

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


* [cv-qualifiers] identifier [= expression]

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

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


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

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


* identifier [ [ constant-expression ] ]

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


char *pch;

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


static unsigned int * const ptr;

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


static int *p = &i, *q = &j;

在前面的示例中,指针 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%pn", 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 %dn", p, *p);

 // changing j will change the result of the indirection
 // operator on p.
 j = 7;
 printf_s("0x%p %dn", 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 %dn", 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 %dn", 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 %dn", 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);
}