[#9] – Pointers – Questions

C-plus-plus-programming-questions-techhyme

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.

Index:

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 –

  1. 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.
  2. The address of the variable can be obtained by preceding the name of the variable with address of operator(&).
  3. Deference operator(*) returns the value of the variable pointed to by the pointer variable.
  4. 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.
  5. 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.
  6. The new operator is used to allocate memory at runtime and returns a pointer to that memory location.
  7. The delete operator is used to deallocate the memory allocated using new operator.

Viva Voce Questions – 

Here are commonly asked questions with answers:

  1. 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].
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. When will you encounter null pointer assignment error ?
    • When you try to perform read or write operation using null pointer.
  7. 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.
  8. 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.
  9. 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 – 

  1. What is a pointer ? Give example.
  2. What are the uses of pointers ?
  3. What is the syntax defining pointer variable ? How a pointer variable is different from ordinary variable ?
  4. Write short note on
    • Reference operator
    • Dereference operator
  5. What do you mean by pointer to a pointer ?
  6. How is one dimensional array implemented using pointers ?
  7. What operations can be performed on pointers ?
  8. Which operations are exclusively used with pointers ?
  9. How can you access a[i][j] element of a 2D array using pointers ?
  10. What is array of pointers ?
  11. How can string be represented using pointer notation ?
  12. Discuss the use of array of strings ?
  13. How are the members of structure accessed when used in conjunction with pointer ?
  14. Explain how a pointer to a function can be declared in C++ ?
  15. Can you pass a function to a function ? If yes, how ?
  16. Why do we need dynamic memory management ?
  17. What is the purpose of new and delete operators ?
  18. What is a void *pointer ? What is its use ?
  19. 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
  20. Do as directed:
    • Write the declaration for the following array using pointers.
      int table [4] [5]
    • Rewrite the following expression by replacing index operator [ ] with the indirection operator(*)
      num[4]
    • Rewrite the following expression by replacing indirection operator(*) with index operator ([ ]).
      *(num + 2)
    • Consider the following declarations.
      int num[10],
      int *p = &num[2];
      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:
      int num[3][4];
You may also like:

Related Posts

Leave a Reply