次の方法で共有


ポインター

ポインターは次の手順を使用して宣言されます。

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

任意の有効なポインターの宣言が declarator に使用される場合があります。単純なポインターの宣言の構文は次のとおりです。:

* [cv-qualifiers] identifier [= expression]

1.宣言の指定子 :

  • 省略可能なストレージ クラスの指定子。詳細については指定子 を参照してください。

  • をポイントするオブジェクトの種類に適用される volatile の省略可能な const またはキーワード。

  • 型指定子 : をポイントするオブジェクトの型を表す型の名前。

2.宣言子 :

  • 省略可能な Microsoft の修飾子。詳細についてはMicrosoft 固有の修飾子 を参照してください。

  • * 演算子です。

  • ポインターに適用する volatile の省略可能な const 自体またはキーワード。

  • 識別子。

  • 省略可能な初期化子。

関数へのポインターの宣言は次のようになります。:

(* [cv-qualifiers] identifier )( argument-list ) [cv-qualifers]
[exception specification] [= expression];
  • ポインターの配列の構文は次のようになります。:
* identifier [ [ constant-expression ] ]
  • ただしポインターの宣言はより複雑になることがあります。詳細については宣言 を参照してください。

  • 複数の宣言と初期化子は宣言の指定子に従ってコンマ区切りのリストで一つの宣言にまとめて表示されることがあります。

ポインターの宣言の例を次に示します。:

char *pch;

上のように宣言はを char 型のオブジェクトへの pch の点で指定します。

複雑な例です

static unsigned int * const ptr;

上のように宣言は ptr が静的ストレージの休暇の種類 unsigned int オブジェクトへの定数ポインターであることを指定します。

複数のポインターを宣言および初期化する方法を次の例に示します :

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

上の例ではp と各ポインター] ij のアドレスへの int 型のオブジェクトへのポインターはとの両方を初期化します。ストレージ クラスの指定子 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++ の抽象宣言子

ポインター型の加算

間接演算子: *

アドレス演算子: と