COMPUTER LANGUAGES HTML,C,C++.JAVA,.NET AND MULTIMEDIA basics and programs click home button
C++ LANGUAGE USING POINTERS
Using Pointers in C++
There are few important operations, which we will do with
the pointers very frequently. (a) We define a pointer variable.
(b) Assign the address of a variable to a pointer.
(c) Finally access the value at the address available
in the pointer variable. This is done by using unary operator
* that returns the value of the variable located at the address
specified by its operand. Following example makes use of these operations:
#include <iostream>
using namespace std;
int main ()
{
int var = 20; // actual variable declaration.
int *ip; // pointer variable
ip = &var; // store address of var in pointer variable
cout << "Value of var variable: ";
cout << var << endl;
// print the address stored in ip pointer variable
cout << "Address stored in ip variable: ";
cout << ip << endl;
// access the value at the address available in pointer
cout << "Value of *ip variable: ";
cout << *ip << endl;
return 0;
}
When the above code is compiled and executed, it produces result something as follows:
Value of var variable: 20
Address stored in ip variable: 0xbfc601ac
Value of *ip variable: 20
No comments:
Post a Comment