Pointers in C: Pointers and Memory Addresses

Before ending this discussion of pointers in C, you should note the details of how they are actually implemented. A computer’s memory can be conceptualized as a sequential collection of storage cells. Each cell of the computer’s memory has a number, called an address, associated with it. Typically, the first address of a computer’s memory is numbered 0. On most computer systems, a “cell” is called a byte.

The computer uses memory for storing the instructions of your computer program, and also for storing the values of the variables that are associated with a program. So, if you declare a variable called count to be of type int, the system assigns location(s) in memory to hold the value of count while the program is executing. This location might be at address 500, for example, inside the computer’s memory.

Luckily, one of the advantages of higher-level programming languages such as C is that you don’t need to concern yourself with the particular memory addresses that are assigned to variables—they are automatically handled by the system. However, the knowledge that a unique memory address is associated with each variable will help you to understand the way pointers operate.

When you apply the address operator to a variable in C, the value that is generated is the actual address of that variable inside the computer’s memory. (Obviously, this is where the address operator gets its name.) So, the statement

intPtr = &count;

assigns to intPtr the address in the computer’s memory that has been assigned to the variable count. So, if count is located at address 500 and contains the value 10, this state- ment assigns the value 500 to intPtr, as depicted in Figure 11.11.

The address of intPtr is shown in Figure 11.11 as –- because its actual value is irrele- vant for this example.

Applying the indirection operator to a pointer variable, as in the expression

*intPtr

has the effect of treating the value contained in the pointer variable  as a memory address. The value stored at that memory address is then fetched and interpreted in accordance with the type declared for the pointer variable. So, if intPtr is of type point- er to int, the value stored in the memory address given by *intPtr is interpreted as an integer by the system. In our example, the value stored at memory address 500 is fetched and interpreted as an integer. The result of the expression is 10, and is of type int.

Storing a value in a location reference by a pointer, as in

*intPtr = 20;

proceeds in a like manner. The contents of intPtr is fetched and treated as a memory address. The specified integer value is then stored at that address. In the preceding state- ment, the integer value of 20 is, therefore, stored at memory address 500.

At times, system programmers must access particular locations inside the computer’s memory. In such cases, this knowledge of the way that pointer variables operate proves helpful.

As you can see from this chapter, the pointer is a very powerful construct in C. The flexibility in defining pointer variables extends beyond those illustrated in this chapter. For example, you can define a pointer to a pointer, and even a pointer to a pointer to a pointer. These types of constructs are beyond the scope of this book, although they are simply logical extensions of everything you’ve learned about pointers in this chapter.

The topic of pointers is probably the hardest for novices to grasp.You should reread any sections of this chapter that still seem unclear before proceeding. Solving the exercis- es that follow will also help you to understand the material.

Source: Kochan Stephen G. (2004), Programming in C: A Complete Introduction to the C Programming Language, Sams; Subsequent edition.

Leave a Reply

Your email address will not be published. Required fields are marked *