Table of Contents
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++;
1 2 3 4 5 6 7 8 9 |
int i=100; float f=9.81; char s[]="this text is in the memory"; std::cout << "adress of integer variable :" << &i << '\n'; std::cout << "adress of floating variable :" << &f << '\n'; std::cout << "adress of string :" << &s << '\n'; |
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,
1 2 3 |
int *p; |
there is 3 syntax of pointer declaration,
1 2 3 4 5 |
int* p; // Preferred int *p; int * p; |
we can define a pointer for a floating number variable as below,
1 2 3 4 5 |
float *f; double *d; |
we can also define for the ASCII strings, char arrays
1 2 3 |
char *s; |
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,
1 2 3 4 5 |
int i; int *p; p = &i; |
Let’s try to understand this in this given example below;
1 2 3 4 5 6 7 8 9 10 11 12 |
int i = 100; // declaring & initializing integer int *p; // pointer declaration p = &i; // address of i is copied to pointer variable cout << "Value of i:" << i << '\n'; cout << "Adress of i:" << &i << '\n'; cout << "Address stored in pointer p: " << p << '\n'; cout << "Value of pointer variable: " << *p << '\n'; |
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,
1 2 3 |
int *p = NULL; |
A 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.
1 2 3 4 5 6 |
int *p = NULL; if ( !p ) // do not use p or break or start with declaring p address in the memory if ( p ) // operate on pointer |