Pointer Operations

I l @ ve RuBoard

Pointer Operations

Just what can you do with pointers? C offers five basic operations you can perform on pointers, and the next program demonstrates these possibilities. To show the results of each operation, we will print the value of the pointer (which is the address it points to), the value stored in the pointed-to address, and the address of the pointer itself. (If your compiler doesn't support the %p specifier , try %u or perhaps %lu for printing the addresses.)

Listing 10.10 shows the five basic operations that can be performed with or on pointer variables .

Listing 10.10 The pt_ops.c program.
 /* pt_ops.c -- pointer operations */ #include <stdio.h> int main(void) {   int urn[3] = {100,200,300};   int * ptr1, * ptr2;   ptr1 = urn;         /* assign an address to a pointer */   ptr2 = &urn[2];     /* ditto                       /* dereference a pointer and take */                       /* the address of a pointer       */   printf("ptr1 = %p, *ptr1 =%d, &ptr1 = %p\n",        ptr1, *ptr1, &ptr1);   ptr1++;            /* increment a pointer             */   printf("values after ptr1++\n");   printf("ptr1 = %p, *ptr1 =%d, &ptr1 = %p\n",        ptr1, *ptr1, &ptr1);   printf("ptr2 = %p, *ptr2 =%d, &ptr2 = %p\n",        ptr2, *ptr2, &ptr2);   ++ptr2;   /* going past end of the array */   printf("values after ++ptr1\n");   printf("ptr2 = %p, *ptr2 = %d, &ptr2 = %p\n",        ptr2, *ptr2, &ptr2);   printf("ptr2 - ptr1 = %d\n", ptr2 - ptr1);                      /* pointer subtraction             */   return 0; } 

Here is the output:

 ptr1 = 0064FDEC, *ptr1 =100, &ptr1 = 0064FDE8 values after ptr1++ ptr1 = 0064FDF0, *ptr1 =200, &ptr1 = 0064FDE8 ptr2 = 0064FDF4, *ptr2 =300, &ptr2 = 0064FDE4 values after ++ptr2 ptr2 = 0064FDF8, *ptr2 = 6618680, &ptr2 = 0064FDE4 ptr2 - ptr1 = 2 

The following list describes the five basic operations that can be performed with or on pointer variables:

  • Assignment.  

    You can assign an address to a pointer. Typically you do this by using an array name or by using the address operator ( & ). In the example, ptr1 is assigned the address of the beginning of the array urn . This address happens to be memory cell number 0064FDEC . The variable ptr2 gets the address of the third and last element, urn[2] .

  • Value-finding (dereferencing).  

    The * operator gives the value stored in the pointed-to location. Therefore, *ptr1 is initially 100 , the value stored at location 0064FDEC .

  • Taking a pointer address.  

    Like all variables, pointer variables have an address and a value. The & operator tells you where the pointer itself is stored. In this example, ptr1 is stored in memory location 0064FDE8 . The content of that memory cell is 0064FDEC , the address of urn .

  • Incrementing a pointer.  

    You can do this by regular addition or by using the increment operator. Incrementing a pointer to an array element makes it move to the next element of an array. Therefore, ptr1++ increases the numerical value of ptr1 by 4 (4 bytes per int on our system) and makes ptr1 point to urn[1] (see Figure 10.2). Now ptr1 has the value 0064FDF0 (the next array address) and *ptr1 has the value 200 , the value of urn[1] . Note that the address of ptr1 itself remains 0064FDE8 . After all, a variable doesn't move around just because it changes value!

    Figure 10.2. Incrementing a type int pointer.
    graphics/10fig02.jpg

    Of course, you can also decrement a pointer. There are some cautions to remember when incrementing or decrementing a pointer, however. The computer does not keep track of whether a pointer still points to an array element. The operation ++ptr2 in this example caused ptr2 to move up another 4 bytes, and now it points to whatever happened to be stored after the array. Also, you can use the increment or decrement operators for pointer variables, but not for address constants, such as array names , just as you can't use the increment operator on regular constants. However, you can use simple addition, as in urn + 2 , for address constants.

    Given the following,

     int urn[3]; int * ptr1, * ptr2; int x, y; 

    here are some valid and invalid statements:

    Valid Invalid
    ptr1++; urn++;
    x++; 3++;
    ptr2 = ptr1 + 2; ptr2 = urn++;
    ptr2 = urn + 1; x = y + 3++;
  • Differencing.  

    You can find the difference between two pointers. Normally, you do this for two pointers to elements that are in the same array to find out how far apart the elements are. The result is in the same units as the type size . For instance, in the output from Listing 10.10, ptr2 - prt1 has the value 2 , meaning these pointers point to objects separated by 2 ints , not by 2 bytes. Subtraction is guaranteed to be a valid operation as long as both pointers point into the same array (or possibly to a position one past the end). Applying the operation to pointers to two different arrays might produce a value or could lead to a runtime error.

These operations open many possibilities. C programmers create arrays of pointers, pointers to functions, arrays of pointers to pointers, arrays of pointers to functions, and so on. Relax, though ”we'll stick to the basic uses we have already unveiled. The first basic use for pointers is to communicate information to and from functions. You already know that you must use pointers if you want a function to affect variables in the calling function. The second use is in functions designed to manipulate arrays. Let's look at a programming example using functions and arrays.

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