In this post we are going to cover some basic and very important pointer questions with answers, which are commonly asked during interviews.
Pointers are the most powerful as well as complex component of C programming. Understanding the concept of pointer is important, because it directly works on address location thus gives you the complete idea of how memory addressing takes place in computer.
These pointer interview questions are prepared to help you understand the basic concept of pointers and its associated keywords.
1. What is pointer?
Pointer is a variable that stores the address of the another variable.
int *ptr;
Here, ptr is a integer type of pointer.
2. What are the usage of pointer in c?
- Used in Dynamic memory Allocation
- To access array elements
- Used to return multiple values
- To pass arguments by reference
- Reduces the execution time of the program
- Pointers helps us to build complex data structures like linked list, stack, queues, trees, graphs etc.
3. Is there any demerits of using pointer?
Uninitialized pointers might cause segmentation fault.
Pointers are slower than normal variables.
4. What is dangling pointer in C?
Pointer pointing to non-existing memory location is called dangling pointer. A dangling pointer is pointer that has a value (not NULL) which refers to some memory which is not valid or not exists.
5. How dangling pointer is different from memory leak?
A dangling pointer points to a memory location that has been deleted or freed.
In opposite of the dangling pointer, a memory leak occurs when you forget to deallocate the allocated memory.
6. How many levels of pointers can you use in a program?
We can use at least 12 indirection.
7. What is a void pointer?
A void pointer is also known as the generic pointer . It can hold address of any type and can be type-casted to any type. A void pointer is declared using void keyword.
8. What is a near, far and huge pointers?
These are very hardware specific concept.
Near pointer is used to store 16 bit addresses means within current segment on a 16 bit machine. The limitation is that we can only access 64kb of data at a time.
A far pointer is typically 32 bit, which includes a segment selector,making it possible to point the addresses outside of the default segment.
huge pointer is also 32 bit and can access outside segment.In far pointer, the segment part cannot be modified, but in Huge it can be.
9. What is a generic pointer?
When a variable is declared as void type it is known as a generic pointer . It is a pointer which can point to any type of data.
10. When we use generic pointer?
Generic pointers are used when we want to return such pointer which is applicable to all types of pointers.
To increase the re-usability of the Pointer it is necessary to declared pointer variable as a generic Pointer.
11. What is double pointer?
When a pointer holds the address of another pointer then such type of pointer is known as pointer-to-pointer or double pointer.
int **ptr;
Here, ptr is a double pointer.
12. What is an array of pointer in c?
An array of pointers is just is an array which holds pointer variables. It is useful for the same reason that all arrays are useful: it allows you to numerically index a large set of variables.
int *ptr[10];
Here, ptr is an array of pointer.
13. Difference between void and null pointer?
When a pointer has initialized with null value it is not pointing anywhere, is called null pointer. Void pointer is a specific pointer of type “ void” – a pointer that points to some data location in storage, which doesn’t have any specific type. So, null pointer is a value, while void pointer is a type.
14. What is the size of generic pointer?
If the system is 16-bit, size of generic pointer would be 2 bytes. If the system is 32-bit, size of generic pointer is 4 bytes. If the system is 64-bit, size of generic pointer is 8 bytes.
15. What is constant pointer in c?
Constant pointer is a pointer variable whose value cannot be altered throughout the program. In other words, we can say that once a constant pointer points to a variable then it cannot point to any other variable.
16. How many bytes are occupied by near, far and huge pointers?
Near Pointer- 2 byte
Far pointer- 4 byte
huge pointer- 4 byte
17. What is wild pointer?
Pointer in programming languages that are not initialized either by the compiler or programmer, known as a wild pointer.
Uninitialized pointers behavior is totally undefined because it may point some arbitrary location that can be the cause of the program crash, that’s is the reason it is called a wild pointer.
18. Can you dereference a void pointer?
No, You can not dereference a void pointer because it doesn’t have a type.
19. What is bad pointer?
When a pointer is first allocated, it does not have a pointee. The pointer is “uninitialized” or simply called as bad pointer.
20. How do you dereference a pointer?
Dereferencing a pointer means getting the value, stored in the memory location pointed by the pointer. The operator * is used to do this, and is called the dereferencing operator.
int k=5;
int* ptr=&k;
printf(“%d”,*ptr); //dereferencing the pointer ptr.
21. What is indirection in C? which operator is used in indirection?
The dereference operator or indirection operator in the context of C, is an operator used to obtain the value of a variable to which a pointer points. In other words, the indirection operator dereferences the pointer and returns the value of the variable at that memory location.
The indirection operator is a unary operator represented by the symbol (*).
22. How pointer is different from array?
An array is a collection of variables of similar data type whereas the pointer is a variable that stores the address of another variable.
23. How to dereference a void pointer?
First you need to cast it to valid variable type, then dereference it.
24. Where the pointer variable are stored in memory?
Pointer variables are stored in stack memory.
25. Is the null pointer same as an Uninitialised pointer?
No, An uninitialized pointer stores an undefined value.
When a pointer is initialized with NULL, it is pointing to 0th memory index. Which is a reserved memory and cannot be dereferenced.
26. What is the data type of a pointer?
The actual data type of the value of all pointers , whether integer, float, character, or others, is the same, a long hexadecimal number that represents a memory address.
27. Can two pointers point to same address?
Yes, Multiple pointers can point to the same same memory address.
28. What are the uses of NULL pointer?
- To initialize a pointer variable.
- To perform error handling with pointers.
- To pass as function argument and to return from a function.
29. Is it possible to add pointers to each other?
No, addition of pointer of two pointers not supported in C. Pointers contain addresses. Adding two addresses makes no sense.
30. What do you mean by pointer to a function?
A pointer to a function points to the address of the executable code of the function. You can use pointers to call functions and to pass functions as arguments to other functions. The type of a pointer to a function is based on both the return type and parameter types of the function.
Recommended: |
View Comments (1)
Very helpful content for interview preparation