Home »
C programming language
C Interview Questions & Answers (Page -3)
Pointer & Dynamic Memory Allocation(DMA)
31) What is a pointer variable?
A pointer is a variable that contains the address in memory of another variable. It takes the same size in memory for any type of pointers.
The size of pointer variable depends on the compiler.
32) What are the advantages & disadvantages of using pointers in C?
Advantages:
- As we know a function can return single value but using pointers we can return multiple values from the functions.
- Generally we declare memory to variables at the compile time but there is a problem either we need more memory or we occupy extra memory space. Using the pointers we can allocate memory to the variable at run time and of course we can resize it while execution time.
- Using pointers we can design complex data structure like Stack Queue, Linked List, and Tree etc.
- Generally we can not change the value of variable in a function (call by value) but using pointers (call by reference) we can change the actual values of the parameters.
- And pointers allow direct memory access.
Disadvantages:
- Pointers allow direct memory access, it can access protected memory locations.
- If pointer is uninitialized, it can be the cause of segmentation fault.
- These are slower than other simple variables.
- Pointers need free dynamically allocated memory.
33) What is a null pointer?
A pointer, that does not point any variable's address is called a NULL pointer. In other words we can say that a NULL pointer is an uninitialized pointer, that may point anywhere.
For example: int *ptr; here, ptr is a NULL pointer, till then it is initialized with the address of any variable. We can also assign to a pointer, like int *ptr=NULL;
34) What is void* (void pointer)?
void* is a special type of pointer, it points the memory address which does not has the specific data type. It is used when you want to point (store) the unknown data type's memory address. You can access the value through a void* after type conversion.
#include < stdio.h >
int main()
{
int a=10;
float b=1.23f;
void* ptr;
ptr=&a;
printf("\na=%d",*(int*)ptr);
ptr=&b;
printf("\nb=%f",*(float*)ptr);
return 0;
}
Output
a=10
b=1.230000
35) What is the difference between NULL pointer and NULL macro?
Both are different, follow the statement
#define NULL 0
Here, NULL is a Macro, and it will be replaced by 0 while preprocessing of the code, it is nothing just a Macro name of value 0.
While, NULL is a pointer that points nowhere memory address i.e. that address which does not exists.
36) Explain the concept of dangling pointer?
A dangling pointer arises when we use the address of an object/ variable after its lifetime is over, or a pointer point to unknown address.
This may occur is situated like returning address of the automatic variable from a function or using the memory block after it is freed.
Consider the following example
#include < stdio.h >
char* displayText(void)
{
char text[]="c programming";
return text;
}
int main()
{
printf("Text returned by function : %s", displayText());
return 0;
}
in above example function will return garbage value, because text is a local variable in function displayText and it is visible and alive for displayText.
37) What is a volatile variable?
Compiler optimizes the variables and stores it into registers as per the frequently usages, if actual value of a variable change, it will not use in expression, volatile keyword tells the compiler that the variable's value may change at any time, so the variable (defined as volatile) should not be optimized by the compiler.
Variables must be accessed the actual variable's storage for each reference, It is generally used with variables and objects that are shared between threads, functions and may change on any interrupt handlers or in device registers.
38) What is a generic pointer?
A pointer declared with type void is known as generic pointer. since there is no variable of void type so void pointer does not point to any specific variable. It can be used to point any type of variable (like int, char, float, struct) after type cast.
Example
#include < stdio.h >
int main()
{
void *ptr;
int a=10;
ptr= (int*) &a;
printf("\nValue of a : %d, Address : %08X\n",*(int*)ptr,(int*)ptr);
return 0;
}
Output
Value of a : 10, Address : 0000FFF4
39) What is a near, far and huge pointers?
Before learning about these pointers, you must know about the memory segments. Memory Segment is the division of computer’s memory (primary) into segments, sections. It is a system to address a computer's memory.
near pointer
A near pointer takes 2 bytes in memory and can point 64KB data segment. It cannot access the data beyond the data segment. You can declare it with the help of near keyword.
far pointer
A far pointer takes 4 bytes in memory and can point whole RAM addresses. The memory outside of the default (data) segment can be accessed/pointed by the pointer declared with far keyword. It can access all 16 segments.
huge pointer
A huge pointer takes 4 bytes in memory. It has an explicit selector, It is same as far pointer. The only difference is that, when you do any pointer arithmetic, it can change the selector. While far pointer can change only offset.
Example
#include < stdio.h >
int main()
{
int near *ptr;
int far *ptr1;
int huge *ptr2;
printf("\n%d,%d,%d",sizeof(ptr),sizeof(ptr1),sizeof(ptr2));
return 0;
}
Output
2,4,4
40) What is indirection in C, what operator is used to indirection and how many levels of indirection can be used for a single declaration?
A simple variable is the direct reference to the memory; it can directly access the value of that variable with out dereferencing or indirection. But pointers are not, if you want to access the value of any memory address you have to use indirection pointer.
Indirection (dereference) operator is "*" , it is used because pointer don’t have direct reference to the memory. A pointer variable stores the address of the variable. To access the value of that memory address using the pointer variable, you have to use * before the pointer variable.
At least 12 indirections are possible for a single declaration.
#include < stdio.h >
int main()
{
int num = 10;
printf("\nBefore indirection, value of num=%d",num);
int *ptr1 = & num;
int **ptr2 = & ptr1;
int ***ptr3 = & ptr2;
int ****ptr4 = & ptr3;
int *****ptr5 = & ptr4;
int ******ptr6 = & ptr5;
int *******ptr7 = & ptr6;
int ********ptr8 = & ptr7;
int *********ptr9 = & ptr8;
int **********ptr10 = & ptr9;
int ***********ptr11 = & ptr10;
int ************ptr12 = & ptr11;
************ptr12 = 20;
printf("\nAfter indirection, value of num=%d",num);
return 0;
}
Output
Before indirection, value of num=10
After indirection, value of num=20