Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

What is the point of using pointers in C++?

+9 votes
asked Dec 20, 2023 by Eidnoxon (5,140 points)
Many people told me "It makes coding easier", but I don't really know HOW and WHY to use pointers. I know, a pointer is basically a variable that points to a specific place in the memory, but how is that useful? Please show me 1 code example that would show my why it's useful, and comment your each step so I could understand.

1 Answer

+2 votes
answered Dec 20, 2023 by Peter Minarik (86,240 points)
edited Dec 20, 2023 by Peter Minarik

Pointers can be super useful in many scenarios.

Pass in arguments by address, not value

Let's assume you want to change the value of a variable inside a function in a way that the caller would know about this change. If the value is passed to this function, a copy is made of the value and inside the function, you only can change the copy. If you want others to know about the change in the original value, you need to pass in the variable by its address. See the below example.

#include <stdio.h>

void incrementBUGGED(int count) { count++; }

void incrementCORRECT(int * count) { (*count)++; }

int main()
{
    int count = 0;
    printf("Count initial value: %d\n", count);
    incrementBUGGED(count);
    printf("Count after bugged increment: %d\n", count);
    incrementCORRECT(&count);
    printf("Count after correct increment: %d\n", count);

    return 0;
}

Another important aspect of passing a variable by address (pointer in C and reference in C/C++) is that multiple functions can work with the same variable, they can modify it, which they would have a hard time to do if you are only allowed to work with copies and not the original (via passing its address).

Making the code run faster

Assume we have a large structure or class that takes up a lot of memory (think of a picture for instance). If you want to pass such a structure or class to a function as an argument, a copy of it is made. This takes time. A much more efficient solution is to pass in the address of this structure instead (which only takes 64 bits on a modern PC, as opposed to the kilobytes or megabytes of data the structure may take).

void displayPicture(const Picture * picture);

Iterate through arrays

You can iterate through arrays (strings are arrays too) efficiently via pointers.

In the below example, we have our own implementation of toUpper() where we start checking the string from its beginning until we reach the terminating zero and keep checking the next character in every iteration. If we find this character to be lowercase, we increment its value to be uppercase. (Note: the increment is actually negative as upper case characters have lower numeric values than lower case numbers, but that's not the point here. XD)

#include <stdio.h>

void toUpper(char * str)
{
    for (char * ch = str; *ch != '\0'; ch++)
    {
        if ('a' <= *ch && *ch <= 'z')
            *ch += 'A' - 'a';
    }
}

int main()
{
    char hello[] = "Hello World!";
    toUpper(hello);
    printf("%s\n", hello);

    return 0;
}

Summary

I hope these few examples allow you to see why pointers are so important in C/C++. Even basic functions like scanf() work by utilising pointers. :)

Keep on coding! :)

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...