Now that you know how a pointer can be used to separate groups of items, I’m going to explain another use of pointers that helps you work with the individual items in the group through a special concept called pointer arithmetic.

You can add or subtract values from your pointer and it will advance or move backwards the appropriate amount based on how many bytes the item type requires. Most computer languages define an int to occupy 4 bytes of memory. If you have an int pointer and its value is 100, that means there should be an integer at the 4 bytes of memory with addresses 100, 101, 102, and 103. You need all 4 of these bytes to make up that single integer. If you now add 1 to your int pointer, its value doesn’t go from 100 to 101, but it goes from 100 to 104. That’s because the next integer must begin at the next memory address which is 104. With an int pointer, you’re counting by fours. This pattern repeats in both directions. That means if you have an int pointer that’s currently pointing to address 100 and you add 3, then it will change to point to address 112. And if you have an int pointer currently pointing to address 100 and you subtract 5, then it will change to point to address 80.

This table shows how a couple integers could be found in memory. The table shows two columns but that’s just for labelling purposes. Computer memory does’t really have columns. It’s just a series of values where each value has an address. So think of the column labelled “Memory Value” as containing the actual values stored in memory while the other column is there just so you can see what the address of each value would be.

Memory Address Memory Value
99 Some byte value
100 Part of int #1
101 Part of int #1
102 Part of int #1
103 Part of int #1
104 Part of int #2
105 Part of int #2
106 Part of int #2
107 Part of int #2
108 Some other byte value