Accessing of the Variables pointed to by the Pointer

Accessing of the variable pointed to by the pointer needs to be understood from two angles, i.e.,

  1. Accessing of normal basic data-type variables
  2. Accessing of class-type objects
  1. Accessing of normal basic data-type variables:
    Let us straight-away learn the accessing of normal basic data-type (line int, float, etc.) variables by seeing a simple example as follows:

    The statement:

    prints the value 20 because *p means the value at the address p.

    Similarly:

    is same as:

    Note that if we write the following statement:

    this prints the address of x. Similarly the statement:

    also prints the addresss of x.

  2. Accessing the class-type objects (Pointer to Object):
    Now, let us understand in brief about accessing of members of an object pointed to by a pointer.Just as you can have pointers to other types of variables, you can have pointers to objects too. When accessing members of a class given a pointer to an object, use the arrow -> operator instead of the dot operator. The operator looks like an arrow and is formed from a “minus sign” and a “greater than” symbol.

    The following program illustrates how to access an object given a pointer to it:

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *