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

How To Use Pointers In C Programming and C++

How To Use Pointers In C Programming and C++

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.

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++;

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,

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

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,

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 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,

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.

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.

What Is Count In C Programming The C++ Builder logo

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.

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

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
Artificial Intelligence TechC++C++17C++20Learn C++

How To Create Simple Generative AI Code In C++

C++C++17Language FeatureLearn C++

How To Use The SVG Image Format With Skia In C++ Builder

C++C++17Language FeatureLearn C++

How To Use Skia Images in C++ Builder?

C++C++17Code SnippetGame DevelopmentLanguage FeatureLearn C++

What Is Skia In Modern C++?