C Programming Interview Questions Set 4
Categories: C Programming language
Ques. What is a far pointer in C?
Ans. A pointer which can access all the 16 segments (whole residence memory) of RAM is known as far pointer. A far pointer is a 32-bit pointer that obtains information outside the memory in a given section.
Ques. What is dangling pointer in C?
Ans. a) If a pointer is pointing any memory location, but meanwhile another pointer deletes the memory occupied by the first pointer while the first pointer still points to that memory location, the first pointer will be known as a dangling pointer. This problem is known as a dangling pointer problem.
b) Dangling pointer arises when an object is deleted without modifying the value of the pointer. The pointer points to the deallocated memory.
Let's see this through an example.
#include<stdio.h>
void main()
{
int *ptr = malloc(constant value); //allocating a memory space.
free(ptr); //ptr becomes a dangling pointer.
}
Ques. What is pointer to pointer in C?
Ans. In case of a pointer to pointer concept, one pointer refers to the address of another pointer. The pointer to pointer is a chain of pointers. Generally, the pointer contains the address of a variable. The pointer to pointer contains the address of a first pointer. Let's understand this concept through an example:
#include <stdio.h>
int main()
{
int a=10;
int *ptr,**pptr; // *ptr is a pointer and **pptr is a double pointer.
ptr=&a;
pptr=&ptr;
printf("value of a is:%d",a);
printf("\n");
printf("value of *ptr is : %d",*ptr);
printf("\n");
printf("value of **pptr is : %d",**pptr);
return 0;
}
In the above example, pptr is a double pointer pointing to the address of the ptr variable and ptr points to the address of 'a' variable.
Ques. What is static memory allocation?
Ans. a) In case of static memory allocation, memory is allocated at compile time, and memory can't be increased while executing the program. It is used in the array.
b) The lifetime of a variable in static memory is the lifetime of a program.
c) The static memory is allocated using static keyword.
d) The static memory is implemented using stacks or heap.
e) The pointer is required to access the variable present in the static memory.
f) The static memory is faster than dynamic memory.
g) In static memory, more memory space is required to store the variable.
For example:
int a[10];
The above example creates an array of integer type, and the size of an array is fixed, i.e., 10.