C - Pointers Question & Answer
Question : What is a pointer?
Answer : Pointers are variables which stores the address of another variable. That variable may be a scalar (including another pointer), or an aggregate (array or structure). The pointed-to object may be part of a larger object, such as a field of a structure or an element in an array.
Question : What are the uses of a pointer?
Answer : Pointer is used in the following cases
i) It is used to access array elements
ii) It is used for dynamic memory allocation.
iii) It is used in Call by reference
iv) It is used in data structures like trees, graph, linked list etc.
Question : What is a NULL Pointer? Whether it is same as an uninitialized pointer?
Answer: Null pointer is a pointer which points to nothing but uninitialized pointer may point to anywhere
What is static memory allocation?
Answer: Compiler allocates memory space for a declared variable. By using the address of operator, the reserved address is obtained and this address is assigned to a pointer variable. This way of assigning pointer value to a pointer variable at compilation time is known as static memory allocation.
What is dynamic memory allocation?
Answer: A dynamic memory allocation uses functions such as malloc() or calloc() to get memory dynamically. If these functions are used to get memory dynamically and the values returned by these function are assigned to pointer variables, such a way of allocating memory at run time is known as dynamic memory allocation.
Question :. What is the purpose of realloc?
Answer: It increases or decreases the size of dynamically allocated array. The function realloc
(ptr,n) uses two arguments. The first argument ptr is a pointer to a block of memory for which
the size is to be altered. The second argument specifies the new size. The size may be increased
or decreased. If sufficient space is not available to the old region the function may create a new