If I (as the compiler) know that an int
is (on this particular system) 4 bytes long, a pointer to an int
need only tell me where the "start" of the int
is: I just need to read it and the next 3 bytes!
For larger data structures like arrays, the same is true: if I know where my array starts, I know that I can access each subsequent element by adding the size of one item to the address. For example, if I have an int a[]
starting at address 100, and an int
is 4 bytes, then
a[32] = (address of a) + (32 * size of int) = 100 + 128 = 228
So bytes 228 through 231 are the integer at a[32].
What makes this slightly easier to use is that the compiler abstracts the different sizes of the data types away for us. If I add 1 to my integer pointer, the address actually increases by 4! This is because I very rarely (almost never) want to read half an integer: it's more likely that I wish to look at a series of integers in turn.