previous | start | next

3. The Address Operator: &

The address operator, & is a unary prefix operator. Its value is the address of its operand and its type is pointer to the type of its operand

For example, given the declaration

          int x = 5;

     

the type of

          &x
       

is pointer to int.

We can declare a variable of this type and use the address operator to initialize it.

          int x = 5;
          int *p = &x;
       

Memory is logically just a sequence of numbered locations, each one able to hold 1 byte of data. The addresses go from 0 to M - 1, where M is the total number of memory bytes available.

In general, we don't need to know the actual address values as numbers in the range 0 to M-1. But it is useful in discussing pointer types to look at some example concrete numbers as addresses.

For example suppose variables x and p are stored in consecutive memory locations. Since memory is byte addressable and an int requires 4 bytes, if x begins at address 4000, it also uses the next 3 bytes as well at 4001, 4002, and 4003. So variable p's value would be stored beginning at address 4004 as indicated in the figure below.



previous | start | next