English

What is a pointer in C++? Give a suitable example. - Computer Science 1

Advertisements
Advertisements

Question

What is a pointer in C++? Give a suitable example.

Very Long Answer
Advertisements

Solution

  1. “A pointer is a variable which holds the memory address of another variable.”
  2. * An operator is used to declare a pointer in C++. It takes the form of:
    datatype *variable_name;
    e.g. int *ptr;
    The above declaration will create a variable ptr, which is a pointer variable and which will point to a variable whose data type is integer.
  3. The data type ptr is "pointer to integer" (int*). It does not store an integer value itself, but stores the memory address where an integer is kept.
  4. Advantages of using pointers are as follows:
    1. It allows passing variables, arrays, functions, strings, structures, and objects as function arguments efficiently.
    2. It allows functions to return multiple values or structured variables.
    3. It supports dynamic memory allocation and deallocation (using new and delete).
    4. By using pointers, variables can be swapped or manipulated without physically moving large amounts of data.
    5. It allows the establishment of links between data elements (used in Data Structures such as Linked Lists and Trees).
shaalaa.com
  Is there an error in this question or solution?
2018-2019 (March) Set 1
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×