adsense


Monday, 23 November 2015

C++ LANGUAGE INCREMENTING A POINTER

COMPUTER LANGUAGES HTML,C,C++.JAVA,.NET AND MULTIMEDIA basics and programs click home button



C++ LANGUAGE INCREMENTING A POINTER




Incrementing a Pointer 

We prefer using a pointer in our program instead of 
an array because the variable pointer can be incremented, 
unlike the array name which cannot be incremented because 
it is a constant pointer. The following program increments 
the variable pointer to access each succeeding element of the array: 

#include <iostream> 
 
using namespace std; 
const int MAX = 3; 
 
int main () 
{ 
   int  var[MAX] = {10, 100, 200}; 
   int  *ptr; 
 
   // let us have array address in pointer. 
   ptr = var; 
   for (int i = 0; i < MAX; i++) 
   { 
      cout << "Address of var[" << i << "] = "; 
      cout << ptr << endl; 
 
      cout << "Value of var[" << i << "] = "; 
      cout << *ptr << endl; 
 
      // point to the next location 
      ptr++; 
   } 
   return 0; 
} 
When the above code is compiled and executed, 
it produces result something as follows: 

Address of var[0] = 0xbfa088b0 
Value of var[0] = 10 
Address of var[1] = 0xbfa088b4 
Value of var[1] = 100 
Address of var[2] = 0xbfa088b8 
Value of var[2] = 200 


No comments: