What is Pointers in C++ ?

  • Pointers are symbolic representation of addresses and it enables a program to simulate call-by-reference as well as to create and manipulate dynamic data structures.
  • It defines a pointer variable and its general declaration in C/C++ has the format.
  • Using unary operator (&) assigning the address of a variable to a pointer which returns the address of that variable.
  • Using unary operator (*) accessing the value stored in the address which returns the value of the variable located at the address specified by its operand.
  • When we increment a pointer, we increase the pointer by the size of data type to which it points, this is the reason data type to a pointer is knows how many bytes the data is stored inside.

Syntax

[pastacode lang=”cpp” manual=”datatype%20*var_name%3B%20%0Aint%20*ptr%3B%0A” message=”” highlight=”” provider=”manual”/]

Sample Code

[pastacode lang=”cpp” manual=”%23include%20%3Ciostream%3E%0Ausing%20namespace%20std%3B%0Aint%20main()%20%7B%0A%09int%20%20x%20%3D%2027%3B%20%20%0A%09int%20%20*ip%3B%20%20%20%20%20%20%20%20%0A%09ip%20%3D%20%26x%3B%20%20%20%20%20%20%20%0A%09cout%20%3C%3C%20%22Value%20of%20x%20is%20%3A%20%22%3B%0A%09cout%20%3C%3C%20x%20%3C%3C%20endl%3B%0A%09cout%20%3C%3C%20%22Value%20of%20ip%20is%20%3A%20%22%3B%0A%09cout%20%3C%3C%20ip%3C%3C%20endl%3B%0A%09cout%20%3C%3C%20%22Value%20of%20*ip%20is%20%3A%20%22%3B%0A%09cout%20%3C%3C%20*ip%20%3C%3C%20endl%3B%0A%09return%200%3B%0A%7D%0A” message=”” highlight=”” provider=”manual”/]

Output

Leave a Reply

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