Manipulating Data by Using Pointers

By: Fazal Emailed: 1645 times Printed: 2110 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
2:
3:     #include <iostream.h>
4:
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";
12:
13:       pAge = &myAge;     // assign address of myAge to pAge
14:
15:       cout << "*pAge: " << *pAge << "\n\n";
16:
17:       cout << "*pAge = 7\n";
18:
19:       *pAge = 7;         // sets myAge to 7
20:
21:       cout << "*pAge: " << *pAge << "\n";
22:       cout << "myAge: " << myAge << "\n\n";
23:
24:
25:       cout << "myAge = 9\n";
26:
27:       myAge = 9;
28:
29:       cout << "myAge: " << myAge << "\n";
30:       cout << "*pAge: " << *pAge << "\n";
31:
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

Comments(0)


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
C++ Destructors Versus Java Finalization
Using Comments in a C++ Program
cin.ignore() in C++
Using cout.width() in C++
Advanced if Statements in C++
while Loops in C++
Multiple statements in for loops in C++
Using switch Statements in C++
Pointers to functions in C++
Sorting an array of Strings in C++
Calculating total based on the given quantity and price in C++
Demonstration of Prefix and Postfix operators in C++
Demonstrating global and local variables in C++
C++ Recursion function explained using Fibonacci series
Most Emailed Articles (in C++)
Returning values from a function in C++
Public versus Private members in C++
while (1) Loops in C++
The indirection operator (*) - dereference operator.
const Pointers in C++
Using Comments in a C++ Program
continue and break statements 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++
Sorting an array of Strings in C++
Specifying default values to function parameters in C++
Dot (.) vs Arrow (->) to access data members in C++
Stray or Dangling Pointers in C++
Two-Dimensional Array Manipulation in C++
Calculating total based on the given quantity and price in C++