5.10.2. Pointers and Operations on Pointers


Creating pointer variables

If you create only one pointer variable in a statement, then creating a pointer variable is just like creating any other variable. For example,

  int* q;
creates a variable called q of type int*. But if you create two or more variables in one statement, you need to be aware of a peculiarity of C++: Each variable that is a pointer needs to have a * in front of it. Statement
  char *r, *s;
creates variables r and s, each of type char*. Statement
  char *r, s;
creates variable r of type char* and s of type char. It does not matter how you space the statement. So
  char* r, s;
still makes s have type char, not char*.


Pointer operations

*p

If p is a pointer then *p is the variable to which p points. For example,
  int v = 1;
  int* p = &v;
  *p = 2;
ends with v = 2, since *p is the same a v.

If p has type T* then *p has type T. For example, pointer p above has type int*. So *p has type int.

q = p;

If you set pointer variable q equal to pointer variable p, then it makes q hold the same memory address as p. So q and p point to the same place. It does not make q point to p.

*q = *p;

Remember that *p is the variable to which p points. Assignment *q = *p sets the variable to which q points equal to the variable to which p points. This is not at all the same as assignment q = p. For example, after
  int x = 1,y = 2;
  int *p = &x
  int *q = &y;
  *p = *q;
variable x holds 2, since *p is the same a x and *q is the same as y.

Pointer conversions

Occasionally, in specific places, you find that you need to change the type of a pointer. Write (T*) in front of a pointer value to tell the compiler to treat that pointer as if it has type T*. But be careful. Changing the type of a pointer does not change anything in the memory. It does no actual conversions. For example,

        int    v = 1;
        int*   p = &v;
        short* q = (short*)(p); 
      
makes q point to the first two bytes of 4-byte variable v. It does not convert v to type short. (What is in the first two bytes of an int depends on the architecture of the processor. A bigendian processor stores the bytes from high order to low order. A littleendian processor stores them from low order to high order. Can you think of an experiment that you could do to find out if the x86 architecture is bigendian or littleendian?)


Type void*

A value of type void* is a pointer to an unknown type of thing. That type is used in some places where a program needs to remember where something is stored without knowing what it points to. Constant NULL is defined to be (void*)(0).


Exercises

  1. Suppose that p is a pointer of type int*. Write a statement that stores 25 into the variable to which p points. Answer

  2. What is the value of variable x after performing the following sequence of statements?

      int x = 50;
      int* p = &x;
      *p = 4;
    
    Answer

  3. What is the value of variable x after performing the following sequence of statements?

      int  y = 7;
      int  x = 35;
      int* p = &x;
      p = &y;
    
    Answer

  4. Suppose that variable p has type Widget*. What is the type of expression *p? Answer