The Address-of Operator

Finding out a variable's address is very simple: C provides the address-of operator (&) to do just that. This operator takes the name of a variable and returns the variable's starting address in memory. For now, think of an address as a big number.

If you have a variable defined as int counter, the correct syntax to get its address is &counter.

Our first example uses the address-of operator to show how variables are laid out in the memory of your computer. The five variables shown in Figure 9.1 will be used in the example so you can see for yourself how your particular environment stores the data.

To find a variable's address

1.
Create a new file or project in your text editor or IDE.

2.
Type the standard beginning lines of code (Script 9.1):

/* pointer1.c - Script 9.1 */
#include <stdio.h>

Script 9.1. This program reveals the memory layout of sample variables.


This application, and working with pointers in general, requires no special libraries, but the standard input and output library is included for using the printf() function.

3.
Begin the main function:

int main (void) {

This function takes no arguments and returns an integer value.

4.
Define and initialize the five variables:

int  a = -40;
int  b = 7853;
char c = 74; 
char d = 19;
int  e = 449378;

These are just five sample variables, of different types. Their values are random, helping to indicate how C stores different value types. Notice that we're using the numeric equivalents of characters when assigning values to the different char variables. See the table in Appendix B, “Resources,” for their character representation.

5.
Use the address-of operator and printf() to display the addresses of the variables:

printf("Address of a: %u
", &a);
printf("Address of b: %u
", &b);
printf("Address of c: %u
", &c);
printf("Address of d: %u
", &d);
printf("Address of e: %u
", &e);

We are using the %u sequence so that printf() displays the addresses as unsigned integer numbers. See the following tip for more on this idea.

6.
Add a getchar() call to pause execution on Windows:

getchar();

7.
Complete the main function:

     return 0;
}

8.
Save the file as pointer1.c, compile, and debug as necessary.

9.
Run the application (Figure 9.2).

Figure 9.2. The actual memory addresses of five different variables on a Windows computer.


The actual addresses revealed by this application are more or less irrelevant. The important thing is to look at the differences from one address to the next.

10.
If possible, recompile and run the application on another computer (Figure 9.3).

Figure 9.3. The results of running the pointer1.c application on another computer, creating different results (because different memory addresses exist).


The example might seem a bit trivial, but it is important to understand these basics as they form the foundation of the material that follows.

✓ Tips

  • In this example we have used the %u placeholder sequence for unsigned integers when printing the addresses. Technically this is not quite correct since an address might not be the same size as an integer on a particular system (although they are on commonly available systems). There is a special placeholder for addresses: %p. That outputs the value as a hexadecimal number, though, which is not as easy to read as a regular decimal integer. If you wish, you can replace %u with %p in the example and run it again to see the output. The remaining examples in this book will use %p.

  • The address-of operator can return the addresses of objects in memory other than variables. For example, it can return addresses of elements in an array, e.g., &(a[9]), and it can even return the addresses of functions, e.g., &my_function (when you define a function, C allocates space in memory for it). You won't need these capabilities nearly as often as you will the ability to point to regular variables.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset