Pointers vs References in C++

C++ Pointers Vs References

In C++, a pointer is a variable that stores the memory address of another variable. Pointers are used to store the addresses of memory locations that can hold a value. Get to learn everything about Pointers Vs References in C++.

On the other hand, a reference is an alias for a variable. When you create a reference, you are creating another name for the same variable. A reference is not a new variable and does not have its own memory location. Instead, it is just an alternative way to refer to the variable.

Pointers vs References in C++

Pointers Vs References in C++

Pointers in C++

In C++, a pointer is a variable that stores the memory address of another variable. Pointers are used to store the addresses of memory locations used by programs, and can be used to access those locations.

Here is an example of declaring a pointer in C++:

int x = 5;
int *ptr;
ptr = &x;

In this example, ptr is a pointer to an int and it stores the memory address of x. The & operator is used to get the memory address of a variable.

To access the value stored at the memory location pointed to by a pointer, you can use the * operator, like this:

int y = *ptr;

In this example, y will be assigned the value of x, which is 5.

References in C++

In C++, a reference is a way to refer to an already existing variable, rather than creating a new one. It is similar to a pointer, but with a few key differences.

Here is an example of declaring a reference in C++:

int x = 5;
int &y = x;

In this example, y is a reference to x. The & operator is used to declare a reference, just as it is used to declare a pointer. However, unlike a pointer, you do not need to use the * operator to access the value of the variable being referred to. You can use the reference itself, like this:

cout << y; // Outputs 5
y = 10; // Changes the value of x to 10

Difference b/w Pointers and References in C++

Pointers in C++References in C++
A reference must be initialized when it is created.A pointer can be initialized later.
A reference cannot be changed to refer to another variable after it is initializedA pointer can be changed to point to different memory locations at any time.
You do not need to use the * operator to access the value of a variable being referred to by a reference.With a pointer, you must use the * operator to dereference the pointer and access the value it points to.
References are often used as function parameters to allow the function to modify the original argumentPointers can also be used for this purpose.
References are generally safer to use than pointers, as they cannot be null and cannot be made to point to invalid memory locations.Pointers can be null and can be made to point to invalid memory locations.

Use of Pointers in C++

Pointers are useful in C++ for a number of reasons. Here are some common scenarios where pointers are used:

  1. To pass arguments to functions by reference, allowing the function to modify the original argument.

  2. To dynamically allocate memory at runtime, using the new operator.

  3. To store the address of an element in an array, allowing you to iterate over the array using pointer arithmetic.

  4. To implement data structures such as linked lists and trees.

  5. To access elements of an array or a string using array notation, even if the array is passed to a function as a pointer.

  6. To pass large structures or arrays to functions more efficiently, by passing a pointer to the structure instead of the entire structure.

Use of References in C++

References are a useful feature of C++, and can be used in a number of situations. Here are some common scenarios where references are used:

  1. As function parameters, to allow the function to modify the original argument. This is often more convenient and easier to read than using pointers for this purpose.

  2. As a more convenient and readable alternative to pointers, especially when you want to avoid the use of pointers or when you want to make it clear that the variable being referred to will not be null.

  3. To improve performance when passing large structures or arrays to functions, by passing a reference to the structure instead of the entire structure.

  4. To create aliases for variables. For example, you might use a reference to give an alternate name to a complex or unwieldy variable, making it easier to work with in your code.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription