Pointers in c++


  • A pointer is an object that contains a memory address. Typically, a pointer is used to access the value of another object.
  • Often this other object is an array. In fact, pointers and arrays are related to each other more than you might expect.
  • The pointer is one of C++’s most powerful features. It is also one of its most troublesome.
  • Despite their potential for misuse, pointers are a crucial part of C++ programming.
  • Ordinarily we need not be concerned about where variables live in the computer’s memory during a program’s execution.
  • The compiler generates machine code that takes care of those details for us.
  • Some systems software like operating systems and device drivers need to access specific memory locations in order to interoperate with hardware.
  • Systems programmers, therefore, must be able to write code that can access such lower-level detail. Developers of higher-level applications sometimes need to access the address of variables to achieve specialized effects.
  • Each byte in a computer’s memory is numbered with a unique address.
  • The first address is 0, and the locations are numbered sequentially up to some maximum value allowed by the operating system and hardware.
  • A C++ variable is stored in memory, so each variable lives at a particular address.

Pointer variable declaration

  • A pointer is an object that contains a memory address.
  • Very often this address is the location of another object, such as a variable. For example, if x contains the address of y, then x is said to “point to” y.

Syntax

    type *var-name; //Here, type is the pointer’s base type.
    int *ip;

The Pointer Operators

  • There are two special operators that are used with pointers: * and &. The & is a unary operator that returns the memory address of its operand.
  • ptr = &total;
  • The second operator is *, and it is the complement of &. It is a unary operator that returns the value of the variable located at the address specified by its operand.
  • val = *ptr;

Example

    #include using namespace std;
    int main()
     {
       int total;
       int *ptr;
       int val;
       total = 3200; // assign 3,200 to total
       ptr = &total; // get address of total
       val = *ptr; // get value at that address
       cout << "Total is: " << val << '
    ';
       return 0;
     } 

Passing by Reference

  • A pass by reference can be programmed using references or pointers as function parameters. It is syntactically simpler to use references, although not always permissible.
  • A parameter of a reference type is an alias for an argument. When a function is called, a reference parameter is initialized with the object supplied as an argument. The function can thus directly manipulate the argument passed to it.
  • Example:
     void test( int& a) { ++a; }

Pointers and Arrays

  • C++ allows pointer arithmetic. Addition and subtraction are allowed with pointers. Suppose you have the following:
  • char array[10];
     char *arrayptr = &array[O]; 
  • In this example, *array_ptr is the same as array[0], *(array_ptr+l) is the same as array[1], and so on. Note the use of parentheses. (*array_ptr)+1 is not the same as array[1].
  • The +1 is outside the parentheses so it is added after the de-reference. Thus (*array_ptr)+1 is the same as array[0] +1.