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.

3. 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 libraries (including iostream) the NULL pointer is a constant with a value of zero defined.

Get started building powerful apps with C++Builder!

close

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.

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++Introduction to C++Language FeatureLearn C++

How To Use The Comprehensive Form Properties In C++ VCL Apps

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

This Is How To Add Characters to Wide Strings in Modern C++

C++ComponentsLanguage FeatureLearn C++

How To Create Gorgeous Transparent App Windows With C++

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

This Is How To Get A Substring of a Wide String in C++

en_USEnglish