C++ Interview Questions - Pointers šŸŽÆ

beginner
21 min

C++ Interview Questions - Pointers šŸŽÆ

Welcome to our comprehensive guide on C++ Pointers! In this lesson, we will dive into one of the core concepts of C++ programming - Pointers. Pointers are variables that store the memory addresses of other variables. They allow you to directly manipulate the memory, which is a powerful feature in C++.

By the end of this lesson, you'll be able to understand and use pointers in your C++ programs confidently. Let's get started! šŸ“

Understanding Pointers šŸ’”

  • A pointer is a variable that stores the memory address of another variable.
  • It is declared by placing an asterisk(*) before the variable name.
  • Pointers allow you to directly manipulate the memory, which can be used for various purposes such as dynamic memory allocation, function parameters, and more.

Declaring a Pointer šŸ“

To declare a pointer, simply place an asterisk(*) before the variable name.

cpp
int num = 10; int *ptr; ptr = # // Assigning the memory address of num to ptr

In the example above, we have declared an integer variable num and a pointer ptr. We have then assigned the memory address of num to ptr using the & operator.

Pointer Types šŸ“

Just like variables, pointers can also be of different types. Here are the main pointer types in C++:

  • int *ptr - Pointer to an int
  • char *ptr - Pointer to a char
  • double *ptr - Pointer to a double
  • void *ptr - Pointer to any data type (generic pointer)

Pointer Arithmetics šŸ“

Pointer arithmetics allow you to perform mathematical operations on pointers, such as addition and subtraction.

  • Addition increments or decrements the pointer to the next or previous memory location of the same data type.
  • Subtraction finds the difference between two pointers in terms of memory locations.
cpp
int arr[] = {1, 2, 3, 4, 5}; int *ptr1 = &arr[0]; // Start from the first element int *ptr2 = &arr[2]; // Start from the third element ptr2 = ptr1 + 2; // Move ptr2 to the third element

In the example above, we have an array arr of integers. We have two pointers, ptr1 and ptr2, which point to the first and third elements of the array, respectively. We then move ptr2 to the third element by adding 2 to ptr1.

Dereferencing a Pointer šŸ“

Dereferencing a pointer means accessing the value stored at the memory address it points to. To dereference a pointer, simply place the asterisk(*) before the pointer name.

cpp
int num = 10; int *ptr = # int value = *ptr; // Access the value stored at the memory address pointed by ptr

In the example above, we have declared a pointer ptr that points to the variable num. We then access the value stored at the memory address pointed by ptr using the asterisk(*).

Pointers and Memory Allocation šŸ’”

Pointers are essential for dynamic memory allocation, which is the process of allocating memory at runtime. In C++, we have functions like new, delete, malloc, and free for memory allocation and deallocation.

cpp
int *ptr = new int(10); // Allocate memory for an int and initialize it to 10 delete ptr; // Deallocate the memory allocated by new

In the example above, we have allocated memory for an integer and initialized it to 10 using new. We then deallocate the memory using delete.

Quick Quiz
Question 1 of 1

What does a pointer do in C++?

Quick Quiz
Question 1 of 1

How do you declare a pointer to an integer in C++?

Quick Quiz
Question 1 of 1

What is the result of `ptr1 + 2` in the following example?