What Is a Pointer in C++?

By: Charles Emailed: 1697 times Printed: 2194 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

Pointers present two special challenges when learning C++: They can be somewhat confusing, and it isn't immediately obvious why they are needed. This article introduces how pointers work, step by step.


Definition of a pointer: A pointer is a variable that holds a memory address.

To understand pointers, you must know a little about computer memory. Computer memory is divided into sequentially numbered memory locations. Each variable is located at a unique location in memory, known as its address.

Different computers number this memory using different, complex schemes. Usually programmers don't need to know the particular address of any given variable, because the compiler handles the details. If you want this information, though, you can use the address of operator (&), which is illustrated in the program below.

Demonstrating address of variables.

1:   // Listing 8.1 Demonstrates address of operator
2:   // and addresses of local variables
3:
4:   #include <iostream.h>
5:
6:   int main()
7:   {
8:      unsigned short shortVar=5;
9:      unsigned long  longVar=65535;
10:     long sVar = -65535;
11:
12:     cout << "shortVar:\t" << shortVar;
13:     cout << " Address of shortVar:\t";
14:     cout <<  &shortVar  _<< "\n";
15:
16:     cout << "longVar:\t"  << longVar;
17:      cout  << " Address of longVar:\t" ;
18:      cout <<  &longVar  _<< "\n";
19:
20:     cout << "sVar:\t"     << sVar;
21:       cout     << " Address of sVar:\t" ;
22:       cout     <<  &sVar     _<< "\n";
23:
24:  return 0;
25: }

Output: shortVar: 5       Address of shortVar: 0x8fc9:fff4
longVar:  65535   Address of longVar:  0x8fc9:fff2
sVar:    -65535   Address of sVar:     0x8fc9:ffee

(Your printout may look different.)


Analysis: Three variables are declared and initialized: a short in line 8, an unsigned long in line 9, and a long in line 10. Their values and addresses are printed in lines 12-16, by using the address of operator (&).

The value of shortVar is 5, as expected, and its address is 0x8fc9:fff4 when run on my 80386-based computer. This complicated address is computer-specific and may change slightly each time the program is run. Your results will be different. What doesn't change, however, is that the difference in the first two addresses is two bytes if your computer uses two-byte short integers. The difference between the second and third is four bytes if your computer uses four-byte long integers.

There is no reason why you need to know the actual numeric value of the address of each variable. What you care about is that each one has an address and that the right amount of memory is set aside. You tell the compiler how much memory to allow for your variables by declaring the variable type; the compiler automatically assigns an address for it. For example, a long integer is typically four bytes, meaning that the variable has an address to four bytes of memory.


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 Charles
arraycopy example in Java
Action listeners in JSF
Automatically Including Preludes and Codas in JSP
Using realloc() Function in C
Handling multiple buttons in HTML Form in Struts
Fopen and Getc implementation program in C
Pointers to Structures example program in C
Pointers vs. Multi-dimensional Arrays in C
getch and ungetch in C
Bitwise Operators in C
Variables and Arithmetic Expressions in C
Differences between JavaServer Faces technology and Struts
assert() example program in C++
'double buffering' Sample program in J2ME
Converting Default Function Arguments in C++ to 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++ )
Dot (.) vs Arrow (->) to access data members in C++
Using Comments in a C++ Program
The indirection operator (*) - dereference operator.
strcpy() and strncpy() sample program in C++
assert() example program in C++
Using cout.width() in C++
Using cout.fill() in C++
Reverse a String in C++
typedef in C++
Calculate average using Two-Dimensional Array in C++
Difference between Procedural, Structured, and Object-Oriented Programming
Demonstration of Prefix and Postfix operators in C++
Constructors and Destructors in C++
while Loops in C++
while (1) Loops in C++
Most Emailed Articles (in C++)
Using cout in C++
Demonstration of Prefix and Postfix operators in C++
qsort() sample program in C++
Using #define, The Preprocessor and the Compiler in C++
Escape Sequences for Nonprintable Characters in C++
A Brief History of C++
C++ and Object-Oriented Programming
Use of Conditional (Ternary) Operator in C++
Returning values from a function in C++
Constructors and Destructors in C++
Interface Versus Implementation in C++
The Stack and the Free Store in C++
this Pointer in C++
How to handle Exceptions in C++
Using cout.width() in C++