C++C++11C++14C++17Learn C++

What Is The Difference Between C++ Pointers and References?

What Is The Difference Between C++ Pointers and References

What is a reference? What is a pointer? What does RVallue and LValue reference mean in C++? What are the differences between a pointer and a reference variable in C++? Let’s find out the answers to these questions. Also, by learning C++ Pointers and References, you can build C++ applications easily with C++ IDE.

What is a Reference in C++?

A reference in the C++ programming language, is a new name for an existing object that stored in an address of memory. In other words, the definition of a reference in C++ is such that it needs to exist.

What is the syntax for a Reference in C++?

Here is the syntax for the reference in C++,

What is a Pointer?

A Pointer is a variable that hold addresses. 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. Please read this post for more about pointers,

What are the differences between a pointer and a reference?

Generally, references are often confused with pointers. There are 3 main differences between references and pointers. These are;

  1. A reference should have an existing object, an object that stored in an address of memory. It cannot have NULL references. It must always be able to assume that a reference is connected to an object in the memory. Pointers can be NULL that means it can be not pointing any object address.
  2. A reference must be initialized when it is created. Pointers can be initialized at any time.
  3. When a reference is initialized to an object, it cannot be changed to refer to another object while pointers can be pointed to another object at any time.

1. A reference should have an existing object

As shown example above a reference needs an existing object to define.

pointers don’t need an existing object, we can initialize when it is needed.

2. A reference must be initialized when it is created

For example this example below give error at the reference declaration,

pointers can be defined null at the beginning and they can be defined at anytime. For example we can define a bitmap pointer and we can initialize this bitmap pointer when use imports a bitmap to our application. When use uploads a new bitmap we can delete this bitmap from the memory and we can initialize a new bitmap to the same pointer. Here is a simple example,

3. When a reference is initialized to an object, it cannot be changed

we can change pointers.

What do RVallue and LValue references mean in C++?

Rvalue References are a compound type like standard C++ references, which are referred to as lvalue references. An lvalue reference is formed by appending the ampersand character (&) to a type

In the C programming language, you can pass arguments only by value. In C++, you can pass arguments by value or by LValue Reference. C++ reference types, closely related to pointer types, create aliases for objects.

A Brief Introduction to the Rvalue References in C++ are explained here and to the LValue References in C++ are explained here in the official DocWiki of Embarcadero. Also RValue and LValue Refereces are explained well here and here in open-std.org.

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.

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

C++C++17Code SnippetLearn C++

How To Convert Char Array String To Unicode String Correctly In C++?

Artificial Intelligence TechC++Language FeatureLearn C++

How To Develop A Simple Hopfield Network In C++