Easy Tutorial: Computer Programming for DUMMIES -- pointer arithmetic

in #programming8 years ago (edited)


Image source

Hey guys! This is another one of my C++ tutorials. Note that what I am going over applies to C as well. This is going to be short and simple! We will be going over pointer arithmetic. Here are my other tutorials if you want to check them out:

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

Part 17: Pointers <-- Definitely check this out before this tutorial if you haven't yet

Pointer Arithmetic

Pointer arithmetic is a way we can transverse data. One example of this is going through an array. If we know what data type we are dealing with, then we know exactly how many bytes we have to travel to get to the next/previous element.

If we have an integer array, then we can access each element using the following method:

int arr[2] = {1,2};
int* ptr = &arr[0];
cout << *ptr << endl;
*ptr++;
cout << *ptr << endl;
  • arr --> an int array containing 1 and 2
  • ptr --> a pointer to the beginning of arr
  • Print the value ptr is currently pointing to
  • Increment ptr
  • Print the value ptr is now pointing to

Output:

1
2
  • By incrementing ptr, we can transverse the array, moving to the next item in the array.

Here is a C program I wrote traversing an array forwards and backwards:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void forward(int *a, int elements)
{
    int x = 0;
    for(x = 0; x < elements; x++)
    {
        printf("%4d ", *a);
        *a++;
    }
    printf("\n");
}
void backward(int *a, int elements)
{
    int x = 0;
    a = a + elements - 1;
    for(x = 0; x < elements; x++)
    {
        printf("%4d ", *a);
        *a--;
    }
    printf("\n");
}

  
int main(void)
{
    int x, arr[10];
    int elements = 10;
  
    srand(time(NULL));
    for (x = 0; x < elements; x++)
        arr[x] = rand() % 10;
  
    forward(&arr[0], elements);
    for(x = 0; x < elements; x++)
        printf("-----");
    printf("\n");
    backward(&arr[0], elements);
}

forward() --> prints the array forwards

  • *a --> a pointer (to the array)
  • elements --> the number of elements in the array
  • increment x for however many elements there are
    • print the value a is pointing to
    • every iteration of the loop, increment *a -- this moves the pointer to the next value in the array
  • backward() --> prints the array backwards

  • *a and elements serve the same purpose
  • a = a + elements - 1 --> this will move the pointer to to the last element
  • increment x for however many elements there are
    • print the value a is pointing to
    • every iteration of the loop, decrement *a -- this moves the pointer to the previous value in the array

main()

  • x --> integer used in for loops
  • arr --> array with 10 values
  • elements --> number of elements in arr (10)
  • sets all elements of arr to random values from 0-9
  • calls forward() -- sends a pointer to the first element of arr and elements
  • prints some lines
  • calls backward() -- sends a pointer to the first element of arr and elements

Output:

[cmw4026@omega test]$ gcc ptr_ar.c
[cmw4026@omega test]$ ./a.out
   8    3    9    2    3    1    6    2    7    0
--------------------------------------------------
   0    7    2    6    1    3    2    9    3    8
[cmw4026@omega test]$

I hope this helped! Leave any suggestions in the comments!

Sort:  

This post has been linked to from another place on Steem.

Learn more about linkback bot v0.4. Upvote if you want the bot to continue posting linkbacks for your posts. Flag if otherwise.

Built by @ontofractal

Coin Marketplace

STEEM 0.18
TRX 0.13
JST 0.028
BTC 57709.05
ETH 3100.20
USDT 1.00
SBD 2.33