What Is a Pointer in C++?
By: Charles
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.
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Subscribe to Tutorials
Related Tutorials
Calculating total based on the given quantity and price in C++
Sorting an array of Strings in C++
Matrix using nested for loops in C++
Compute the square root of the sum of the squares of an array in C++
Calculate average using Two-Dimensional Array in C++
Two-Dimensional Array Manipulation in C++
Compiling and Linking Multiple Source Files in C++
Escape Sequences for Nonprintable Characters in C++
Using the Built-in Arithmetic Types in C++