adsense


Monday, 23 November 2015

C++ LANGUAGE DECREMENTING A POINTER

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



C++ LANGUAGE DECREMENTING A POINTER






Decrementing a Pointer 


The same considerations apply to decrementing a pointer, 
which decreases its value by the number of bytes of its data type as shown below: 

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


No comments: