C++Introduction to C++Learn C++

Learn to Use Pointers and Memory Address of a Variable in C++

1. Memory Address of a Variable

The RAM (Random Access Memory) is the memory of a computer that can be read and changed in any order, typically used to store working data and machine codes. In some operations, we use ROM (Read Only Memory), In our applications, we have a lot of variables and every variable stored in the RAM and it takes space in the memory, These variables have a memory location and every memory location has its address defined which can be accessed by using ampersand (&) operator. This operator denotes an address in memory. 

Here below we give an example of the address of 3 most used types in C++;

2. Pointers

Pointers are variables that hold addresses and the asterisk character ‘*’ is used to define pointers, they are used before the variable name. Pointers are some of the strongest aspects of the C & C++ programming languages. They allow you to reach any kind of type (including very long size bitmaps or videos or other data etc.) without copying the whole data.

For example, we can define a pointer for an integer variable as below,

there is 3 syntax of pointer declaration,

we can define a pointer for a floating number variable as below,

we can also define for the ASCII strings, char arrays

3. Using Pointers

We use ampersand ‘&’ operand to reach address of variable and we can basically we set address of variables to pointer by using this & operator. Let’s explain this with a simple integer value,

Let’s try to understand this in this given example below;

Here we declare and initialize an integer variable i and we also declare a pointer p. Then we copied the address of the i variable to the pointer variable by using p = &i;
Next, we print out the value of i and address of i by using &i.
Finally, we print out the address of p (which must be the same as the address of i) and we print out the value in that address by using *p.

In the beginning, these & * symbols and using pointer might be difficult to understand, you need to use them more, so you can memorize these symbols and meanings for a variable usage.

4. Using Null Pointers

Generally pointers are automatically NULL at the beginning but in some compilers it might have value, so it should be NULL at the beginning as below,

NULL Pointer is a pointer that is assigned NULL, and it means it is a pointer that has no address value. Most of the operations about pointers, always check if a pointer is NULL or not. If you don’t check then you must be sure that it is not NULL in that line in any condition. For example, you may try to copy something to a pointer address while it is NULL, your application will give an error or might have some dangerous behaviors, stuck of the app, reset of the app, etc.

In many standard library headers (including iostream) the NULL pointer is a constant with a value of zero defined.

Get started building powerful apps with C++Builder!

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?