指针

使用以下顺序,指针声明为。

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

其中任何有效的指针声明为可以为 declarator使用。 一个简单的指针声明的语法如下所示:

* [cv-qualifiers] identifier [= expression]

1. 声明说明符:

  • 一种选项存储类说明符。 有关更多信息,请参见 说明符

  • 适用于将所指向的对象的类型的可选 const 或 volatile 关键字。

  • 类型说明符:表示对象的类型类型的名称将点。

2. 声明:

  • 一个选项特定于 Microsoft 的修饰符。 有关更多信息,请参见 特定于 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%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);
}
  

另一个示例阐释如何在数据结构的指针;在这种情况下,联接表。

// 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');
}
  

请参见

参考

C++抽象声明

指针类型的添加

间接寻址运算符:*

address-of运算符:&