Add or subtract an integral value and a pointer. - C++ Data Type

C++ examples for Data Type:Pointer

Description

Add or subtract an integral value and a pointer.

Demo Code

#include <iostream>
using namespace std;
#define P(EX) cout << #EX << ": " << EX << endl;
int main() {//from   w w  w  .ja  v a 2s.co  m
   int a[10];
   for(int i = 0; i < 10; i++)
      a[i] = i; // Give it index values
   int* ip = a;
   P(*ip);
   P(*++ip);
   P(*(ip + 5));
   int* ip2 = ip + 5;
   P(*ip2);
   P(*(ip2 - 4));
   P(*--ip2);
   P(ip2 - ip); // Yields number of elements
}

Result


Related Tutorials