icp/oer/courses/c-basics/sections/10-pointer/04-pointer-arithmetic/content.html

15 lines
848 B
HTML
Raw Normal View History

2018-05-05 22:18:02 +00:00
<p>
When you are using pointers, you sometimes actually don't want to talk about the address the pointer points to.
Maybe you want to talk about the <b>address next to it</b>. Or you want to get the <b>distance between to pointers</b> in memory.
This is what pointer arithmetic allows you to do. You can use <b>mathematical operations on the addresses</b> of pointers.
All the operations are done relative to the size of the type the pointer refers to.
</p>
<p>
For example, if you have an pointer to a long long, which is 8bytes big, and add one to the pointer,
you will get an address 8bytes from your original pointer.
If you are using an int, you will get one 4bytes from the original.
</p>
<p>
Arrays store all the data next to another in memory. Try using pointer arithmetic to print out all the characters in the array.
</p>