Storing the Address in a Pointer

By: Daniel Malcolm Emailed: 1675 times Printed: 2157 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

Every variable has an address. Even without knowing the specific address of a given variable, you can store that address in a pointer.

For example, suppose that howOld is an integer. To declare a pointer called pAge to hold its address, you would write

int *pAge = 0;

This declares pAge to be a pointer to int. That is, pAge is declared to hold the address of an int.

Note that pAge is a variable like any of the variables. When you declare an integer variable (type int), it is set up to hold an integer. When you declare a pointer variable like pAge, it is set up to hold an address. pAge is just a different type of variable.

In this example, pAge is initialized to zero. A pointer whose value is zero is called a null pointer. All pointers, when they are created, should be initialized to something. If you don't know what you want to assign to the pointer, assign 0. A pointer that is not initialized is called a wild pointer. Wild pointers are very dangerous.

 


NOTE: Practice safe computing: Initialize your pointers!

If you do initialize the pointer to 0, you must specifically assign the address of howOld to pAge. Here's an example that shows how to do that:

unsigned short int howOld = 50;     // make a variable
unsigned short int * pAge = 0;      // make a pointer
pAge = &howOld;                     // put howOld's address in pAge

The first line creates a variable--howOld, whose type is unsigned short int--and initializes it with the value 50. The second line declares pAge to be a pointer to type unsigned short int and initializes it to zero. You know that pAge is a pointer because of the asterisk (*) after the variable type and before the variable name.

The third and final line assigns the address of howOld to the pointer pAge. You can tell that the address of howOld is being assigned because of the address of operator (&). If the address of operator had not been used, the value of howOld would have been assigned. That might, or might not, have been a valid address.

At this point, pAge has as its value the address of howOld. howOld, in turn, has the value 50. You could have accomplished this with one less step, as in

unsigned short int howOld = 50;       // make a variable
unsigned short int * pAge = &howOld;  // make pointer to howOld

pAge is a pointer that now contains the address of the howOld variable. Using pAge, you can actually determine the value of howOld, which in this case is 50. Accessing howOld by using the pointer pAge is called indirection because you are indirectly accessing howOld by means of pAge. Later today you will see how to use indirection to access a variable's value.


New Term: Indirection means accessing the value at the address held by a pointer. The pointer provides an indirect way to get the value held at that address.

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(2)


1. View Comment

please show many codes in c++....thankZz

View Tutorial          By: Maybelline Hope Bongolan at 2008-08-20 17:50:51
2. View Comment

Hi Maybelline, This is the <a href="http://www.java-samples.com/showtitles.php?category=C%2B%2B&start=1">list of C++ sample code and tutorials</a>

View Tutorial          By: Daniel Malcolm at 2008-08-21 06:57:31

Your name (required):


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


Your sites URL (optional):


Your comments:



More Tutorials by Daniel Malcolm
javac options in Java
Operator Precedence in Java
Calling Multiple Listeners in JSF
Using free() Function in C
ForwardAction in Struts
Listing Files and Directories sample program in C
Binary Tree - (Self-referential Structures) example program in C
A simple program using EL in JSP
Command-line Arguments in C
Example Calculator program in C - describing use of External Variables in C
Assignment Operators and Expressions in C
The for statement in C
JSF Basics
assert() Versus Exceptions in C++
RMS Basics in J2ME

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++ )
Public versus Private members in C++
Dot (.) vs Arrow (->) to access data members in C++
strcat() and strncat() sample program in C++
Two-Dimensional Array Manipulation in C++
const Member Functions in C++
while (1) Loops in C++
while loop in C++
Calculate average using Two-Dimensional Array in C++
Sorting an array of Strings in C++
Calculating total based on the given quantity and price in C++
Using Comments in a C++ Program
The indirection operator (*) - dereference operator.
How to handle Exceptions in C++
assert() example program in C++
cin.ignore() in C++
Most Emailed Articles (in C++)
Looping with the keyword goto in C++
Difference between Procedural, Structured, and Object-Oriented Programming
Operator Precedence in C++
Using peek() and putback() with cin in C++
Function overloading in C++
Default arguments in C++
Lvalues and Rvalues in C++
Compiling and Linking Multiple Source Files in C++
C++ and Object-Oriented Programming
Getting Started with C++
Tutorial on Classes and Members in C++
Public versus Private members in C++
for Loops in C++
Using switch Statements in C++
How to handle Exceptions in C++