Important questions on pointers

1. What are pointers?
Ans. A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address. The general form of a pointer variable declaration is −
data_type *var_name;
2. What is the usage of the pointer in C?
Ans. Following are the main uses of pointers:

  • Accessing array elements: Pointers are used in traversing through an array of integers and strings. The string is an array of characters which is terminated by a null character ‘\0’.
  • Dynamic memory allocation: Pointers are used in allocation and deallocation of memory during the execution of a program.
  • Call by Reference: The pointers are used to pass a reference of a variable to other function.
  • Data Structures like a tree, graph, linked list, etc.: The pointers are used to construct different data structures like tree, graph, linked list, etc.

 3.What is a NULL pointer? 
Ans: NULL is used to indicate that the pointer doesn’t point to a valid location. Ideally, we should initialize pointers as NULL if we don’t know their value at the time of declaration. Also, we should make a pointer NULL when memory pointed by it is de-allocated in the middle of a program.
 4. What is Dangling pointer?
Ans:Dangling Pointer is a pointer that doesn’t point to a valid memory location. Dangling pointers arise when an object is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory. Following are examples.

int *ptr = (int *)malloc(sizeof(int));
// ptr is a dangling pointer now and operations like following are invalid
*ptr = 10;  // or printf(“%d”, *ptr);
int *ptr = NULL
int x  = 10;
ptr = &x;
// x goes out of scope and memory allocated to x is free now.
// So ptr is a dangling pointer now.

5. What is a pointer on pointer?
Ans. It’s a pointer variable which can hold the address of another pointer variable. It de-refers twice to point to the data held by the designated pointer variable.
Eg: int x = 5, *p=&x, **q=&p;
Therefore ‘x’ can be accessed by **q.
6. What is a pointer to a function? Give the general syntax for the same.
Ans. A pointer holding the reference of the function is called pointer to a function. In general it is declared as follows.
T (*fun_ptr) (T1,T2…); Where T is any date type.
Once fun_ptr refers a function the same can be invoked using the pointer as follows.
7. Can a pointer access the array?
Ans. Pointer by holding array’s base address can access the array.
8.What is a constant pointer?
Ans. A pointer which is not allowed to be altered to hold another address after it is holding one.
9. To make pointer generic for which date type it need to be declared?
Ans. void
10. What is the difference between near, far and huge pointers?
Ans. A virtual address is composed of the selector and offset.
near pointer doesn’t have explicit selector whereas far, and huge pointers have explicit selector. When you perform pointer arithmetic on the far pointer, the selector is not modified, but in case of a huge pointer, it can be modified.
These are the non-standard keywords and implementation specific. These are irrelevant in a modern platform.

Related Articles

Core Java

1. What are primitive types in Java ? byte, short, int, long, float, double, char, boolean… 2. What are tokens? Name the 5 types of tokens available in Java with an example…

C Programming

1. Wap to check if the given number is armstrong number or not. #include<stdio.h> #include<conio.h> void main(){ int a,t,l,sum=0; printf(“\nPlease enter your number\n”); scanf(“%d”,&a); t=a;…

Advance Java

1. Write an Example Snippet to Connect Java Application with mysql database. 2. Wap to get the object of ResultSetMetaData. The getMetaData() method of ResultSet…


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

KodNest Training New Batch is starting on 21st September 2020. Attend one week free demo classes.Register Now

New Report