Pointers are one of the most powerful features available in C++. The use of pointers offers a great degree of flexibility for manipulating data in programs and making programs quicker and memory efficient.
- [#1] – Introduction to C++ – Questions
- [#2] – Preparing and Running C++ Program – Questions
- [#3] – C++ Fundamentals – Questions
- [#4] – Data Input and Output – Questions
- [#5] – Control Structures – Questions
- [#6] – Functions – Questions
- [#7] – Arrays and Strings – Questions
- [#8] – Structures and Unions – Questions
- [#9] – Pointers – Questions
- [#10] – Object and Classes – Questions
- [#11] – Constructors and Destructors – Questions
- [#12] – Operator Overloading and Type Conversion – Questions
- [#13] – Inheritance – Questions
- [#14] – Virtual Functions – Questions
- [#15] – C++ Streams and Files – Questions
- [#16] – Templates – Questions
- [#17] – Exception Handling – Questions
Most of the learners feel that understanding pointer is a very daunting task, but it is not so. Although programming can be done without the use of pointers but in some situations, pointers show its real power such as
- Management of objects allocated memory dyanamically
- Passing arrays, strings to functions more efficiently
- To pass function arguments that will be changed by the function
- To return more than one value from the function
- To pass address of structures or objects instead of entire object to functions
- Creation of linked data structures such as Linked Lists, Trees, Graphs etc
- Optimize the program to execute faster or use less memory.
Key Points To Remember –
- A pointer is a variable that contains the address of another variables. Pointer variables are also known as address variables as they always store address of some other variable.
- The address of the variable can be obtained by preceding the name of the variable with address of operator(&).
- Deference operator(*) returns the value of the variable pointed to by the pointer variable.
- Pointer to a pointer means that a variable can be declared which can store the address of a pointer variable. They offer a great degree of flexibility for handling arrays, passing pointer variables to functions and passing command line arguments.
- When an integer value is added to a pointer variable, the pointer variable is not incremented by that integer value instead it is incremented by address stored currently in pointer variable plus increment value multiplied by sizeof type to which pointer variable points to.
- The new operator is used to allocate memory at runtime and returns a pointer to that memory location.
- The delete operator is used to deallocate the memory allocated using new operator.
Viva Voce Questions –
Here are commonly asked questions with answers:
- Can we write a[i] as i[a] ?
- Yes,since a[i] can be accessed using pointer notation as *(a+i) which is equivalent to *(i+a). Thus in turn equal to i[a].
- What is the difference between ++s→a and s++→a ?
- The first one increments the contents of data member a and not s structure variable. However, in the second statement, structure variable s is incremented after accessing data member a.
- What is dangling reference ?
- It refers to a situation when a portion of memory referred through pointer is released (in the free pool) but pointer still point to it. It happens when memory is not released explicitly.
- What is the difference between int *const ptr=i; and const int * const ptr=i; ?
- The first one defines a constant pointer to integer i and you cannot modify the pointer. But in the second one. you cannot modify the pointer as well as integer to which integer is pointing.
- What is memory leak ?
- It is a block of dynamically allocated memory that we no longer have a pointer pointing to it and thus we cannot return it to the program for later reuse. It is usually encountered when we forget to delete dynamically allocated array.
- When will you encounter null pointer assignment error ?
- When you try to perform read or write operation using null pointer.
- What is the difference between pointer to arrays and array of pointers ?
- Pointer to array refers to using a single pointer to access elements of an array whereas array of pointers refers to collection of pointer variables that hold the address of different array elements.
- What does the following declaration mean:
type (*fp)() and type *fp()
- In first case, fp is a pointer function that accepts no arguments and return type. In the second case, fp is a function that accepts no arguments and returns a pointer to type.
- What is null pointer ?
- C++ defines a special value called null pointer for each pointer type. It is used to compare a pointer to any object or function. The internal representation of null pointer for different data type may be different.
Other Similar Questions –
- What is a pointer ? Give example.
- What are the uses of pointers ?
- What is the syntax defining pointer variable ? How a pointer variable is different from ordinary variable ?
- Write short note on
- Reference operator
- Dereference operator
- What do you mean by pointer to a pointer ?
- How is one dimensional array implemented using pointers ?
- What operations can be performed on pointers ?
- Which operations are exclusively used with pointers ?
- How can you access a[i][j] element of a 2D array using pointers ?
- What is array of pointers ?
- How can string be represented using pointer notation ?
- Discuss the use of array of strings ?
- How are the members of structure accessed when used in conjunction with pointer ?
- Explain how a pointer to a function can be declared in C++ ?
- Can you pass a function to a function ? If yes, how ?
- Why do we need dynamic memory management ?
- What is the purpose of new and delete operators ?
- What is a void *pointer ? What is its use ?
- Write ‘T’ for True and ‘F’ for false against the following statements.
- Pointers can be constants.
- The underlying type of a pointer is address.
- The indirection(*) operator is uses with a pointer to dereference the address contained in the pointer.
- int *ptr= &x; defines and initializes a pointer to the address of x.
- *ptr++; will add 1 to a variable ptr.
- Pointers whose contents are the address of another pointer is pointers to pointers.
- Adding 1 to a pointer increases the address stored in it by 1 byte.
- When a void pointer is dereferenced, it must be cast.
- It is possible to cast a pointer to integer as pointer to double.
- The name of the array is a pointer variable.
- Dynamically allocated memory can be referred only through pointers.
- Pointer arithmetic is only valid with pointers to arithmetic variables.
- int a;
int *p = &a; is valid
- int *p;
cin >>&p; is valid
- Do as directed:
- Write the declaration for the following array using pointers.
int table  
- Rewrite the following expression by replacing index operator [ ] with the indirection operator(*)
- Rewrite the following expression by replacing indirection operator(*) with index operator ([ ]).
*(num + 2)
- Consider the following declarations.
int *p = &num;
How will you access the fifth element of array num using the pointer p.
- Write a function declaration for a function data that accepts the whole array num using a pointer. The array num is declared as follows:
- Write the declaration for the following array using pointers.