Opening the book…
Introduction to C++ Pointer
In C++ programming, a pointer is a special variable that can only be used for holding an address of a memory cell.
What is an address?
In a C++ program, when you use following statement to declare a variable:
int a;
or
char a;
you allocate a piece of memory cell on the computer memory card to your program so you can store a data (either an integer or a character) in such memory cell.
There are three types of information that are associated with each memory cell:
- the name (such as a in above example)
- content (the data stored in the memory cell, sometimes it may not have a content, such as the one in above example)
- address which is the physical address of such memory cell to indicate where the memory cell is located on the computer memory card.
A memory cell on a computer memory card is like a building in a city. Each building in a city have three type of information:
- The name of the building (such as Walmart, CVS and so on)
- The content (what is inside of the building)
- Street address of the building (such as 1101 E. University Ave)
A pointer is a variable that is specifically for storing the address of memory cell used in a computer program.
Now, let us get familiar with pointer.
In a program, we may use following statement to declare pointer variable:
int *p;
where p is the name of the pointer, it represents a memory cell that can be used for storing the address of an integer data. Similarly,
char *ch;
declares a pointer ch that represents a memory cell which can be used for storing the address of a character data
Once a pointer is declared in a program, we can use it to store the address of a variable:

Line 6: we declare an integer pointer ptr and a regular variable a with the initial value 5.
Line 7: '&' is an extract operator which extracts the address of the variable a and the statement, ptr=&a; assignd the address of a to the pointer ptr.
Line 8: this statement displays the address of the variable a:

Once a point hold an address of a variable in a program, such as ptr is holding the address of the variable a in above program, then *ptr is same as the variable a, which means *ptr and a represent the same memory cell. We use following program to explain this:

Since we assign the address of a to the pointer ptr in Line 7, the ptr is holding the address of the variable a, thus, *ptr is the same as the variable a. In Line 8, we assign integer 15 to *ptr, this is the same as assigning 15 to the variable a. Therefore, when we use a cout statement to display the value of the variable a, we should see its value has been changed to 15, following is a screenshot of the output of the program:

To help you to better understand points, let's study following sample program:

Line 6: We declared two pointers and two variables and initialized the variables to 5 and 20 respectively.
Line 7 and Line 8: We assigned the addresses of the variables a and b to pointer ptr1 and ptr2 respectively.
Line 9: We copy the address in the pointer ptr1 into pointer ptr2, thus, at this point, both ptr1 and ptr2 are holding the address of the variable a (both ptr1 and ptr2 are pointing to a). Therefor both *ptr1 and *ptr2 are the same as a.
Line 10: We assigned integer 10 to *prt2, which is the same as assigned 10 to a.
Line 11: Display the value of a on the output screen, which is 10
Line 12: Display the value of b on the output screen, which is 20, because after Line 9, ptr2 is no longer pionting to b, instead, it is pointing to a. Therefore, the statement in Line 10 did not alter the value of the variable b, instead, it alter the value of a from 5 to 10, that is why the value of b is remained as 20. Following is the screenshot of output of the program:

This page titled 9.1: What is a Pointer? was last modified on Wed, 24 Dec 2025 20:37:20 GMT and is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Jin He.
The book could not be loaded just now.