From the course: Complete Guide to C Programming Foundations

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Doing pointer math

Doing pointer math

- [Instructor] A pointer is a variable that holds a memory location. So what happens when you add 1 to its value? Here I'm referring to the pointer variable as an address without the asterisk operator. So in this example in this exercise file, you see pointer pa initialized to the address of variable alpha at line 8. Line 9 displays the address stored in pa, the address of variable alpha, and then line 10 displays that address plus 1. Let's see what happens. Doing my best hexadecimal math, I can see that these two values differ by four bytes, which is the size of an integer variable on this machine. Adding 1 to the address stored in pa increased the address by four bytes. By the way, at this point in line 10, the pointer references some unknown chunk of memory. This is unsafe coding practice, but I did it to illustrate a point. Here's a better example. Array twos is defined and contains five elements. Pointer pt is set to the base address of the array at line 8. No ampersand is…

Contents