Easy Tutorial: Computer Programming for DUMMIES -- pointers


Image source

Hey guys! This tutorial will be about pointers in C++ (but will easily transfer to C). You can use pointers to accomplish very neat things in programming. This is part 17 of my programming tutorials. If you want to catch up on the rest of them, here they are:

Part 1: Hello World!

Part 2: Variables

Part 3: Functions

Part 4: if(), else, else if()

Part 5: Loops

Part 6: Arrays

Part 7: Basic Input/Output

Part 9: Sorting Arrays

Part 10: Random Numbers

Part 11: Colored Text in your Terminal

Part 12: Recursion

Part 13: Binary Search

Part 14: 2D Arrays

Part 15: String Processing

Part 16: Binary, Bitwise Operators, and 2^n

Introduction to Pointers

Each variable created contains a memory address. You can access the memory address by placing '&' before the variable.

int x = 4;
cout << &x;

This prints the address of x. The address will look something like this:

0x7fffdf78f634

When you create a pointer, you simply put a '*' before the variable name.
For:

int x = 4;

You can declare a pointer with any of the following statements:

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

It really doesn't matter, but the first three are preferred. If we want to access the information in the pointer, use a '*' when referencing it. If you want to access the memory address, don't include the symbol.

cout << *ptr << endl;
cout << ptr;

Output:

4
(some address)

Here is a good picture that illustrates how pointers work:

Image source

Here is a program I wrote to demonstrate the use of pointers:

#include<iostream>
using namespace std;

int main()
{
    int integer = 5;

    cout << "The contents of integer: " << integer << endl;
    cout << "The address of integer: " << &integer << endl << endl;

    float number = 69.75;
    cout << "The contents of number: " << number << endl;

    float* num_pointer = &number;
    cout << "The contents of num_pointer: " << num_pointer << endl;
    cout << "The contents of *num_pointer: " << *num_pointer << endl;

    return 0;
}

Output:

[cmw4026@omega test]$ ./a.out
The contents of integer: 5
The address of integer: 0x7fffc86c814c

The contents of number: 69.75
The contents of num_pointer: 0x7fffc86c8148
The contents of *num_pointer: 69.75
[cmw4026@omega test]$

Why Pointers are Useful

You may not see the point of pointers.... Okay, I'm not that funny! But seriously... Why would we need a pointer if we can just reference the original variable to get what we want done? Well, one example is that we can pass pointers to functions. This is very efficient. Instead of having to pass a value, such as an integer, to a function, then passing the new value for the integer back, we can simply hand it a pointer instead. The function can then change the value at the memory address (where the integer is stored), and when we reference the variable in main again, the value is changed! If this is a little hard to grasp, here is a simple program I wrote to show this method in action:

#include<iostream>
using namespace std;

void change_value(int* x)
{
    *x = 250;
}

int main()
{
    int a = 500;
    int* ptr= &a;

    cout << "'a' before change_value(): " << a << endl;
    change_value(ptr);
    cout << "'a' after change_value(): " << a << endl;

    return 0;
}

Output:

[cmw4026@omega test]$ g++ pointer2.cpp
[cmw4026@omega test]$ ./a.out
'a' before change_value(): 500
'a' after change_value(): 250
[cmw4026@omega test]$
  • I printed the same variable, but after change_value() was called, the value was different.

I hope this tutorial helped you understand pointers! The syntax for pointers is the exact same in C. Leave suggestions in the comments!

Image source

Sort:  

But pointers are also very dangerous! Be careful!

Just don't try something if you don't know what it does!

Coin Marketplace

STEEM 0.18
TRX 0.13
JST 0.028
BTC 57497.94
ETH 3099.12
USDT 1.00
SBD 2.32