C Language Pointers Questions

C Language Pointers Questions

1. What are Pointers?

A pointer is a variable whose value is the address of another variable i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before you can use it to store any variable address. Pointers are used frequently in C, as they have a number of useful applications. For example, pointers can be used to pass information back and forth between a function and its reference point. The pointers in ‘C’ language increase the efficiency of program to a large extent. Although they are difficult to use but it is a very powerful tool while managing the memory.
The actual data type of the value of all pointers, whether integer, float, character, or otherwise, is the same, a long hexadecimal number that represents a memory address. The only difference between pointers of different data types is the data type of the variable or constant that the pointer points to. The unary or monadic operator ‘&’ gives the “address of a variable”. The indirection or dereference operator ‘*’ gives the “contents of an object pointed to by a pointer”.

2. What is the purpose of a pointer?
Pointers are used for many reasons like:
•    Pointers reduce the length and complex city of program.
•    They increase the processing speed.
•    They save the memory to a very large extent.
•    A pointer enables to access any variable whether it may be outside the function i.e. a direct control over a variable can be made using pointers.

3. What is the basic syntax of a pointer?
Pointer variables, like all other variables, must be declared before; they may be used in C program. When a pointer variable is declared, the variable name must be preceded by an asterisk (*). This identifies the fact that the variable is a pointer. The data type that appears in the declaration refers to the object of the pointer. i.e. the data item that is stored in the address represented by the pointer, rather than the pointer itself. Thus a pointer declaration may be written in general terms as:
Data-type *ptr;
Where ptr is the name of the pointer variable, and data-type refers to the data type of the pointer object.
For example, a C program contains the following declarations.
int i,*ptri;
float f,*ptrf;
The first line declares ‘i’ to be an integer type variable and ptri to be a pointer variable whose object is an integer quantity. The second line declares f to be a floating-point type variable and ptrf to be a pointer variable whose object is a floating point quantity.
Within a variable declaration, a pointer variable can be initialized by assigning in the address of another variable, remember that the variable whose address is assigned to the pointer variable must have been declared earlier in the program, for example,
int i;
int *ptri=&i;
The first line declares i to be integer type variable and the second line declares ptri to be a pointer variable whose object is an integer point quantity. In addition, the address of i is initially assigned to ptri.

4. How to use a pointer in C?

A pointer can be used in two contexts.
•    For accessing the address of the variable whose memory address the pointer stores. consider the following code:
 char ch = ‘c’;
char *chptr = &ch;
Now, whenever we refer the name ‘chptr’ in the code after the above two lines, then compiler would try to fetch the value contained by this pointer variable, which is the address of the variable (ch) to which the pointer points, i.e. the value given by ‘chptr’ would be equal to ‘&ch’.
For example:
char *ptr = chptr;
The value held by ‘chptr’ (which in this case is the address of the variable ‘ch’) is assigned to the new pointer ‘ptr’.
•    For accessing the value of the variable whose memory address the pointer stores. Continuing with the piece of code used above :
char ch = ‘c’;
char t;
char *chptr = &ch;
t = *chptr;
We see that in the last line above, we have used ‘*’ before the name of the pointer. This operator when applied to a pointer variable name (like in the last line above) yields the value of the variable to which this pointer points. Which means, in this case ‘*chptr’ would yield the value kept at address held by chptr. When used with pointers, the asterisk ‘*’ operator is also known as ‘value of’ operator.

5. What is a far pointer? Where we use it?

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.

6. What is a huge pointer?

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.

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

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.

8. What is near pointer?

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.

9. What is generic pointer in C?

In C void* acts as a generic pointer. When other pointer types are assigned to generic pointer, conversions are applied automatically (implicit conversion).

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

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.

11. What is a NULL Pointer? Whether it is same as an uninitialized pointer?

Null pointer is a pointer which points to nothing but uninitialized pointer may point to anywhere.

12. What does the error ‘Null Pointer Assignment’ means and what causes this error?

As null pointer points to nothing so accessing an uninitialized pointer or invalid location may cause an error.

13. Are pointers integer?

No, pointers are not integers. A pointer is an address. It is a positive number.

14. What are the pointer declarations used in C?

•    Array of pointers, e.g., int *a [10]; Array of pointers to integer
•    2-Pointers to an array, e.g., int (*a) [10]; Pointer to an array of into
•    3-Function returning a pointer, e.g., float *f ( ); Function returning a pointer to float
•    4-Pointer to a pointer, e.g, int **x; Pointer to a pointer to int
•    5-pointer to a data type, e.g, char *p; pointer to char

15. What is pointer to a pointer?

If a pointer variable points another pointer value. Such a situation is known as a pointer to a pointer.
int *p1,**p2,v=10;
P1=&v; p2=&p1;
Here p2 is a pointer to a pointer.

16. What is an array of pointers?

Array is a collection of multiple data items which are represented by using a single identifier. Since a single identifier representing all the data items will have generally a continuous allocation in arrays there is one base address to which all the contiguous memory location are attached. These memory locations are having then difference in addresses and their addresses are continuous in manner.
Since the array is having a base address and all the memory location is continuous in manner and they store the data item of some type and the scalar factor can be used in the pointers. Hence if a pointer variable is made pointing to the base address of the any array then all successive element of that array can be access by incrementing the pointer variable till the end of the array. In general terms, a two-dimensional array can be defined as a one-dimensional array of pointers by:
data-type *array [expression 1]                   
int main()
int a [10], *p;
P=&a [0]; [0]
P=& a [10] [10]
This will assign the base address of array ‘a’ in the pointer variable p. Now if the value of the second index has to be accessed then the pointer p is incremented and now it is pointing to the next index to the base address. This can be done by using increment operator like p++.

17. What is pointer arithmetic?

C pointer is an address which is a numeric value. Therefore, you can perform arithmetic operations on a pointer just as you can a numeric value. There are four arithmetic operators that can be used on pointers: ++, –, +, and -.
To understand pointer arithmetic, let us consider that ptr is an integer pointer which points to the address 1000. Assuming 32-bit integers, let us perform the following arithmetic operation on the pointer:
Now after the above operation, the ptr will point to the location 1004 because each time ptr is incremented, it will point to the next integer location which is 4 bytes next to the current location. This operation will move the pointer to next memory location without impacting actual value at the memory location. If ptr points to a character whose address is 1000, then above operation will point to the location 1001 because next character will be available at 1001.

18. What is the invalid pointer arithmetic?

i) Adding, multiplying and dividing two pointers.
ii) Shifting or masking pointer.
iii) Addition of float or double to pointer.
iv) Assignment of a pointer of one type to a pointer of another type.

19. What is pointer incrementation?

A pointer variable is incremented by using an increment operator then its address value is not increased by 1 but it will be increased by a complete address this type of incrementation is called pointer incrementation and the length which pointer is incrementing is called scalar factor.

20. How are pointers compared?

Pointers may be compared by using relational operators, such as ==, <, and >. If p1 and p2 point to variables that are related to each other, such as elements of the same array, then p1 and p2 can be meaningfully compared.

21. Define Passing pointers to functions in C?

Pointers are often passed to a function as arguments. This allows the data items within calling portion of the program to be accessed by the function, altered within the function, and returned to the calling portion of the program in altered form.
When an argument is passed by value, the data item is copied to the function. Thus, an alteration made to the data item within the function is not carried over into calling routine. When an argument is passed by reference, however (i.e. when a pointer is passed to a function), the address of a data item is passed to the function. The contents of that address can be accessed freely, either within the function or within the calling routine. Moreover, any change that is made to the data item will be recognized in both the function and the calling routine. Thus, the use of a pointer as a function argument permits the corresponding data item to be altered globally from within the function.
When pointers are used as arguments to a function, some care is required with the formal argument declarations within the function. Specifically, formal pointer arguments that must each be preceded by an asterisk. Function prototypes are written in the same manner. If a function declaration does not include variable names, the data type of each pointer argument must be followed by an asterisk.
A function pointer can be declared as:
<return type of function> (*<name of pointer>) (type of function arguments)
For example:
int (*fptr) (int, int)
The above line declares a function pointer ‘fptr’ that can point to a function whose return type is ‘int’ and takes two integers as arguments.

22. How pointer variables are initialized?

Pointer variables are initialized by one of the following ways.
I. Static memory allocation
II. Dynamic memory allocation

23. What is static memory allocation?

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.

24. What is dynamic memory allocation?

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. The functions malloc (), realloc (), calloc () and free (), the Library functions stdlib.h, malloc.h or are responsible for this task. All data are stored in the free store (heap), which is limited to 64k a stack in the beginning, this varies from machine.

25. What is the purpose of realloc?

With the function realloc, you can change the size of the allocated area once. Has the following form.
void * realloc (void * ptr, size_t size);
The first argument specifies the address of an area that is currently allocated to the size in bytes of the modified second argument. Change the size, the return value is returned in re-allocated address space. Otherwise it returns NULL.
The address of the source address changed, but the same could possibly be different, even if the different areas of the old style, because it is automatically released in the function realloc, for the older areas it is not necessary to call the free function. However, if the function fails and returns NULL, realloc, the older area is to remain still valid. Therefore, the first pointer argument of the function realloc, both can be NULL pointer return value is not returned.

26. What is the purpose of malloc?

The malloc () function dynamically allocates memory when required. This function allocates ‘size’ byte of memory and returns a pointer to the first byte or NULL if there is some kind of error. Format is as follows.
void * malloc (size_t size);
Specifies in bytes the size of the area you want to reserve the argument. It returns the address as the return value of the dynamically allocated area. In addition, returns NULL if it fails to secure the area. The failure to ensure that the situation is usually that is out of memory. The return type is of type void *, also receive the address of any type. The fact is used as follows.
double * p = (double *) malloc (sizeof (double));

27. What is the purpose of calloc?

The calloc function is used to allocate storage to a variable while the program is running. This library function is invoked by writing calloc (num,size).This function takes two arguments that specify the number of elements to be reserved, and the size of each element in bytes and it allocates memory block equivalent to num * size . The function returns a pointer to the beginning of the allocated storage area in memory. The important difference between malloc and calloc function is that calloc initializes all bytes in the allocation block to zero and the allocated memory may/may not be contiguous. Calloc function is used to reserve space for dynamic arrays. Has the following form.
void * calloc (size_t n, size_t size);
Number of elements in the first argument specifies the size in bytes of one element to the second argument. A successful partitioning, that address is returned, NULL is returned on failure.
For example, an int array of 10 elements can be allocated as follows.
int * array = (int *) calloc (10, sizeof (int));

28. Is the allocated space within a function automatically deallocated when the function returns?

No pointer is different from what it points to .Local variables including local pointers variables in a function are deallocated automatically when function returns., But in case of a local pointer variable ,deallocation means that the pointer is deallocated and not the block of memory allocated to it. Memory dynamically allocated always persists until the allocation is freed or the program terminates.

29. Differentiate between a constant pointer and pointer to a constant?
const char *p; //pointer to a const character.
char const *p; //pointer to a const character.
char * const p; //const pointer to a char variable.
const char * const p; // const pointer to a const character.

30. Are the expressions *ptr ++ and ++ *ptr same?

No,*ptr ++ increments pointer and not the value pointed by it. Whereas ++ *ptr increments the value being pointed to by ptr.


This site uses Akismet to reduce spam. Learn how your comment data is processed.