Advertisements
Advertisements
प्रश्न
What is a pointer in C++? Give a suitable example.
सविस्तर उत्तर
Advertisements
उत्तर
- “A pointer is a variable which holds the memory address of another variable.”
*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 variableptr, which is a pointer variable and which will point to a variable whose data type is integer.- The data type
ptris "pointer to integer" (int*). It does not store an integer value itself, but stores the memory address where an integer is kept. - Advantages of using pointers are as follows:
- It allows passing variables, arrays, functions, strings, structures, and objects as function arguments efficiently.
- It allows functions to return multiple values or structured variables.
- It supports dynamic memory allocation and deallocation (using
newanddelete). - By using pointers, variables can be swapped or manipulated without physically moving large amounts of data.
- It allows the establishment of links between data elements (used in Data Structures such as Linked Lists and Trees).
shaalaa.com
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
