C Language

Pointers Question & Answer



Question : 1

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 : 2

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 : 3

What are the differences between malloc () and calloc ()?



Answer :

Malloc Calloc 1-Malloc takes one argument Malloc(a);where a number of bytes 2-memory allocated contains garbage values

1-Calloc takes two arguments Calloc(b,c) where b no of object and c size of object

2-It initializes the contains of block of memory to zerosMalloc takes one argument, memory allocated contains garbage values.

It allocates contiguous memory locations. Calloc takes two arguments, memory allocated contains all zeros, and the memory allocated is not contiguous.



Question : 4

Difference between pass by reference and pass by value?



Answer :

Pass by reference passes a pointer to the value. This allows the callee to modify the variable directly.Pass by value gives a copy of the value to the callee. This allows the callee to modify the value without modifying the variable. (In other words, the callee simply cannot modify the variable, since it lacks a reference to it.)



Question : 5

What is a far pointer? Where we use it?



Answer :

In large data model (compact, large, huge) the address B0008000 is acceptable because in these model all pointers to data are 32bits long. If we use small data model(tiny, small, medium) the above address won‘t work since in these model each pointer is 16bits long. If we are working in a small data model and want to access the address B0008000 then we use far pointer. Far pointer is always treated as a 32bit pointer and contains a segment address and offset address both of 16bits each. Thus the address is represented using segment : offset format B000h:8000h. For any given memory address there are many possible far address segment : offset pair. The segment register contains the address where the segment begins and offset register contains the offset of data/code from where segment begins.



Question : 6

What is a huge pointer?



Answer :

Huge pointer is 32bit long containing segment address and offset address. Huge pointers are normalized pointers so for any given memory address there is only one possible huge address segment: offset pair. Huge pointer arithmetic is doe with calls to special subroutines so its arithmetic slower than any other pointers.



Question : 7

What is a normalized pointer, how do we normalize a pointer?



Answer :

It is a 32bit pointer, which has as much of its value in the segment register as possible. Since a segment can start every 16bytes so the offset will have a value from 0 to F. for normalization convert the address into 20bit address then use the 16bit for segment address and 4bit for the offset address. Given a pointer 500D: 9407,we convert it to a 20bitabsolute address 549D7,Which then normalized to 549D:0007.



Question : 8

What is near pointer?



Answer :

A near pointer is 16 bits long. It uses the current content of the CS (code segment) register (if the pointer is pointing to code) or current contents of DS (data segment) register (if the pointer is pointing to data) for the segment part, the offset part is stored in a 16 bit near pointer. Using near pointer limits the data/code to 64kb segment.



Question : 9

In C, why is the void pointer useful? When would you use it?



Answer :

The void pointer is useful because it is a generic pointer that any pointer can be cast into and back again without loss of information.



Question : 10

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.



CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online Exam Quiz O Level NIELIT Study material and Quiz Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Best Website and Software Company in Allahabad Website development Company in Allahabad