Finding Addresses: The Operator

I l @ ve RuBoard

Finding Addresses: The & Operator

One of the most important C concepts (and sometimes one of the most perplexing) is the pointer , which is a variable used to store an address. You've already seen that scanf() uses addresses for arguments. More generally , any C function that modifies a value in the calling function without using a return value uses addresses. We'll cover this topic next , beginning with the unary & operator.

The unary & operator gives you the address at which a variable is stored. If pooh is the name of a variable, then &pooh is the address of the variable. You can think of the address as a location in memory. Suppose you have the following statement:

 pooh = 24; 

Suppose that the address where pooh is stored is 0B76 . (PC addresses often are given as four-digit hexadecimal values.) Then the statement

 printf("%d %p\n", pooh, &pooh); 

would produce this ( %p is the specifier for addresses):

 24 0B76 

In Listing 9.13, let's use this operator to see where variables of the same name ”but in different functions ”are kept.

Listing 9.13 The loccheck.c program.
 /* loccheck.c  -- checks to see where variables are stored */ #include <stdio.h> void mikado(int);                      /* declare function */ int main(void) {     int pooh = 2, bah = 5;     printf("In main(), pooh = %d and &pooh = %p\n",             pooh, &pooh);     printf("In main(), bah = %d and &bah = %p\n",             bah, &bah);     mikado(pooh);     return 0; } void mikado(int bah)                   /* define function  */ {     int pooh = 10;     printf("In mikado(), pooh = %d and &pooh = %p\n",             pooh, &pooh);     printf("In mikado(), bah = %d and &bah = %p\n",             bah, &bah); } 

Listing 9.13 used the %p for printing the addresses. This format specifier is used for displaying addresses but is not available on some pre-ANSI systems. If your system lacks %p , try %u or, perhaps, %lu . On our system, this is the output of this little exercise:

 In main(), pooh = 2 and &pooh = 0064FDF0 In main(), bah = 5 and &bah = 0064FDF4 In mikado(), pooh = 10 and &pooh = 0064FDE0 In mikado(), bah = 2 and &bah = 0064FDEC 

The way that %p represents addresses varies between implementations . This one (Microsoft Visual C++ 5.0 on a PC) displays the address in hexadecimal form.

What does this output show? First, the two pooh s have different addresses. The same is true for the two bah s. So, as promised , the computer considers them to be four separate variables. Second, the call mikado(pooh) did convey the value ( 2 ) of the actual argument ( pooh of main() ) to the formal argument ( bah of mikado() ). Note that just the value was transferred. The two variables involved ( pooh of main() and bah of mikado() ) retain their distinct identities.

We raise the second point because it is not true for all languages. In a FORTRAN subroutine, for example, the subroutine affects the original variable in the calling routine. The subroutine's variable might have a different name, but the address is the same. C doesn't do this. Each function uses its own variables. This is preferable because it prevents the original variable from being altered mysteriously by some side effect of the called function. However, it can make for some difficulties, too, as the next section shows.

I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net