Manipulating Data by Using Pointers

By: Fazal Emailed: 1742 times Printed: 2431 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Once a pointer is assigned the address of a variable, you can use that pointer to access the data in that variable. Program below demonstrates how the address of a local variable is assigned to a pointer and how the pointer manipulates the values in that variable.

Manipulating data by using pointers.

1:     // Listing 8.2 Using pointers
3:     #include <iostream.h>
5:     typedef unsigned short int USHORT;
6:     int main()
7:     {
8:        USHORT myAge;         // a variable
9:        USHORT * pAge = 0;    // a pointer
10:       myAge = 5;
11:       cout << "myAge: " << myAge << "\n";
13:       pAge = &myAge;     // assign address of myAge to pAge
15:       cout << "*pAge: " << *pAge << "\n\n";
17:       cout << "*pAge = 7\n";
19:       *pAge = 7;         // sets myAge to 7
21:       cout << "*pAge: " << *pAge << "\n";
22:       cout << "myAge: " << myAge << "\n\n";
25:       cout << "myAge = 9\n";
27:       myAge = 9;
29:       cout << "myAge: " << myAge << "\n";
30:       cout << "*pAge: " << *pAge << "\n";
32:    return 0;
33: }

Output: myAge: 5
*pAge: 5

*pAge = 7
*pAge: 7
myAge: 7

myAge = 9
myAge: 9
*pAge: 9

Analysis: This program declares two variables: an unsigned short, myAge, and a pointer to an unsigned short, pAge. myAge is assigned the value 5 on line 10; this is verified by the printout in line 11.
On line 13, pAge is assigned the address of myAge. On line 15, pAge is dereferenced and printed, showing that the value at the address that pAge stores is the 5 stored in myAge. In line 17, the value 7 is assigned to the variable at the address stored in pAge. This sets myAge to 7, and the printouts in lines 21-22 confirm this.

In line 27, the value 9 is assigned to the variable myAge. This value is obtained directly in line 29 and indirectly (by dereferencing pAge) in line 30.

C++ Home | All C++ Tutorials | Latest C++ Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Bookmark and Share


Be the first one to add a comment

Your name (required):

Your email(required, will not be shown to the public):

Your sites URL (optional):

Your comments:

More Tutorials by Fazal
The Tag Life Cycle with Attributes in JSP
Using Shift Operators in C
IncludeAction in Struts
typedef example program in C
Arithmetic Evaluation Using the Expression Language in JSP
Tutorial on Complicated Declarations in C
Static Variables in C
getPosition() the right strategy in getting GPS
Precedence and Order of Evaluation in C
File Copying in C
JSF Life Cycle
Getting User Input Using cin in C++
UDP Datagram sample in J2ME
GUI components and menu based J2ME Applications.
A Serialization Example in Java

More Tutorials in C++
Two-Dimensional Array Manipulation in C++
Calculate average using Two-Dimensional Array in C++
Compute the square root of the sum of the squares of an array in C++
Matrix using nested for loops in C++
Sorting an array of Strings in C++
Calculating total based on the given quantity and price in C++
Compiling and Linking Multiple Source Files in C++
Enumerations in C++
Program to add two numbers in C++
Comments in C++
while loop in C++
for loop in C++
Programming errors a compiler will detect in C++
if in C++
Using the Built-in Arithmetic Types in C++

More Latest News
Most Viewed Articles (in C++ )
Difference between Procedural, Structured, and Object-Oriented Programming
assert() example program in C++
Dot (.) vs Arrow (->) to access data members in C++
Constructors and Destructors in C++
Using cout in C++
while (1) Loops in C++
for loop in C++
Calculate average using Two-Dimensional Array in C++
Calculating total based on the given quantity and price in C++
Using Comments in a C++ Program
class keyword in C++
Converting Pointer Parameters in C++ to Java
Converting Default Function Arguments in C++ to Java
Operator Precedence in C++
Using cout.width() in C++
Most Emailed Articles (in C++)
const Member Functions in C++
Printing values in DEBUG mode in C++
if in C++
Returning values from a function in C++
Public versus Private members in C++
atoi(), itoa() in C++
qsort() sample program in C++
Pass by Reference in C++ functions
Pass by Reference vs Pass Value in C++ functions
Demonstration of Prefix and Postfix operators in C++
Where to Put Class Declarations and Method Definitions in C++
continue and break statements in C++
C ++ Reference Parameters Versus Java Reference Parameters
Getting User Input Using cin in C++
Function overloading in C++