POINTERS:


Pointers represent a specialized type of variable capable of storing the memory address of another variable. They hold memory addresses rather than the values of variables. In C++, pointers play a crucial role in programming, facilitating certain tasks more efficiently. Understanding pointers is key, and we can illustrate their concept through the following example.

Imagine we ask someone to deliver a parcel to Omar's house. Initially, our reference point is Omar's name. However, if we provide the person with the house number and street address, we're referencing by the address.

When we declare a variable in C++, it reserves space in the computer's memory, each location possessing a unique address accessible using the ampersand &operator. Let's consider the following example, which demonstrates printing the address of defined variables.

C++ Copy to Clipboard  
#include <iostream>
using namespace std;

int main() {
    int a, b;
    double c;
    float d;

    cout << "The Address of a is: " << &a << endl;
    cout << "The Address of b is: " << &b << endl;
    cout << "The address of c is: " << &c << endl;
    cout << "The address of d is: " << &d << endl;

    return 0;
}

In C++, declaring a pointer involves prefixing its name with an asterisk *. Additionally, we specify the pointer's type, indicating the type of data stored at the address it will hold. For instance:

int* ptr;
int *ptr;
int*ptr;
int * ptr;

Pro-Tip:

The spacing around the asterisk is insignificant; each statement above declares an integer pointer.

In these declarations, ptrdenotes our variable's name, while the asterisk *signals the compiler to allocate memory for storing an address. intsignifies our intent to use the pointer to store the address of an integer variable, making it point to an integer.

After declaring a pointer variable, such as ptr, and a variable, let's say "x," we often need to initialize the pointer. To accomplish this, we use the address operator &to assign the address of "x" to ptr, like so:.

ptr = &x;

The address operator &retrieves the memory address of "x" and assigns it to the pointer ptr, effectively making ptrpoint to "x."

Experienced C++ programmers leverage pointers for various purposes, including substituting array names. Since both array names and pointers hold memory addresses, a pointer can effectively hold an array name as long as their types match.

Consider the following example, where we declare two variables, assign values, and attempt to modify their values using pointers:

C++ Copy to Clipboard  
#include <iostream>
using namespace std;

int main() {
    int x = 5, y = 15;
    int *ptr;

    cout << "Before: x = " << x << " | y = " << y << endl;

    ptr = &x;
    *ptr = 10;

    ptr = &y;
    *ptr = 20;

    cout << "After: x = " << x << " | y = " << y << endl;

    return 0;
}

In C++, "NULL" serves as a constant value indicating null pointers, typically defined in various C++ libraries, including <iostream>. Should it not be defined, we can define it as zero ourselves.

Before a pointer receives an assignment, it contains an arbitrary value. Attempting to use such an uninitialized pointer could lead to program crashes. To mitigate this risk, C++ programmers conventionally assign the null value to unused pointers, ensuring they don't inadvertently point to anything. Initializing pointers to null helps prevent unintended use.

To check for a null pointer, we utilize if-statements:

if (ptr == NULL);
if (!ptr);

if (ptr != NULL);
if (ptr);

Assigning NULL to a pointer variable during declaration is a good practice when the exact address is unknown, as demonstrated below:

C++ Copy to Clipboard  
#include <iostream>
using namespace std;

int main() {
    int *ptr = NULL;
    cout << "The value of Ptr is : " << ptr << endl;

    return 0;
}