What Is a Pointer in C++?
By: Charles in C++ Tutorials on 2007-09-14
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.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- 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
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++
Comments