Pointers are variables that hold addresses and the asterisk character ‘*’ is used to define pointers, it is used before the variable name. This variable can be an integer, float, string, or any data block like structs, object arrays, bitmaps coming from a camera, or a signal or sound wave coming from an input, any data block that has a memory address can be pointed by a pointer. If there are no memory allocations for them, pointers can be null too. Any C or C++ compiler versions and C++ standards can be used to use pointers in C and C++ codes. Pointers are some of the strongest aspects of the C & C++ programming languages.
In this post, we will explain how to use pointers in C and professionally in C++ too.
Table of Contents
How to use memory adress of pointers In C programming
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'; |
What is a pointer In C programming
Pointers are variables that hold addresses and the asterisk character ‘*’ is used to define pointers, it is 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; |
How to use pointers in C programming
We use ampersand ‘&
‘ operand to reach the address of a 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 pointers might be difficult to understand, but with practice you’ll start to remember these symbols and their meanings for variable usage.
How to use NULL pointers in C programming
Generally, pointers are automatically NULL at the beginning but in some compilers, it might have value – often, a random value – so pointers should be set to 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. When performing operations on pointers 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 otherwise unwanted behavior can occur, sometimes with significantly bad results. 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, the app may freeze, the app might restart, or, on some operating systems it might even cause issues with the operating system.
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 7 8 9 10 |
int *p = NULL; // Examples of use.. if ( !p ) { // if P is null then execute the code block }; if ( p ){ // if P points to somewhere other than NULL then execute the code block }; |
How to use pointers in C++ programming ?
The C++ standards support the most of features of previous C++ standards including C language. One of the features is pointers from C. While there are many modern ways to use pointers, C pointers can be used in C++ as given examples above too. Here are some more interesting topics about to use pointers with some other features in C++.
Although the free C++ Builder Community Edition is extremely powerful it is intended for students, beginners, and startups. If you are a regular business or do not qualify for the free community edition, then you can download a free trial of the very latest full RAD Studio C++ Builder version.
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition