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 - C Tutorial
From the course: Complete Guide to C Programming Foundations
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
-
-
-
-
-
-
-
-
-
-
Understanding pointers3m 23s
-
(Locked)
Using pointers to manipulate data2m 53s
-
(Locked)
Doing pointer math3m 10s
-
(Locked)
Comparing arrays and pointers5m 5s
-
(Locked)
Challenge: Output a string57s
-
(Locked)
Solution: Output a string2m 44s
-
(Locked)
Allocating storage3m 42s
-
(Locked)
Working with a pointer array4m
-
(Locked)
Using pointers in structures3m 8s
-
(Locked)
Challenge: Allocating structures1m 3s
-
(Locked)
Solution: Allocating structures3m 34s
-
(Locked)
Passing pointers to functions2m 23s
-
(Locked)
Returning pointers from a function2m 31s
-
Chapter challenge: Processing input1m 26s
-
(Locked)
Chapter solution: Processing input3m 14s
-
-
-
-