Interview questions for pointer
1. What is indirection?
If you declare a variable, its name is a direct reference to its value. If you have a pointer to a variable, or any other object in memory, you have an indirect reference to its value. If p is a pointer, the value of p is the address of the object. *p means “apply the indirection operator to p”; its value is the value of the object thatp points to. (Some people would read it as “Go indirect on p.”)
*p is an lvalue; like a variable, it can go on the left side of an assignment operator, to change the value. Ifp is a pointer to a constant, *p is not a modifiable lvalue; it can’t go on the left side of an assignment.
Consider the following program. It shows that when p points to i, *p can appear wherever i can.
2. What is a null pointer?
There are times when it’s necessary to have a pointer that doesn’t point to anything. The macro NULL, defined in <stddef.h>, has a value that’s guaranteed to be different from any valid pointer. NULL is a literal zero, possibly cast to void* or char*. Some people, notably C++ programmers, prefer to use 0 rather thanNULL.
You can’t use an integer when a pointer is required. The exception is that a literal zero value can be used as the null pointer. (It doesn’t have to be a literal zero, but that’s the only useful case. Any expression that can be evaluated at compile time, and that is zero, will do. It’s not good enough to have an integer variable that might be zero at runtime.)
3. When is a null pointer used?
The null pointer is used in three ways:
1. To stop indirection in a recursive data structure.
2. As an error value.
3. As a sentinel value.
5. What is a void pointer?
A void pointer is a C convention for “a raw address.” The compiler has no idea what type of object a voidpointer “really points to.” If you write
ip points to an int. If you write
p doesn’t point to a void!
In C and C++, any time you need a void pointer, you can use another pointer type. For example, if you have a char*, you can pass it to a function that expects a void*. You don’t even need to cast it. In C (but not in C++), you can use a void* any time you need any kind of pointer, without casting. (In C++, you need to cast it.)
4. Difference between array and pointer in C
Array and pointer are different from each other. Below I am mentioning some points which describe the difference between array and pointer in C language.
1. An array is a collection of elements of similar data types whereas pointer is a variable that store the address.
2. Array element store at contiguous memory location whereas pointer can store one address at a time.
3. When we use the sizeof the operator with the array it gives the total number of bytes which used by the elements whereas pointer only gives the size of the pointer.
5. What are the advantages of using an array of pointers to string instead of an array of strings?
An array of pointers to string is useful when sorting the strings, we need to swap only pointers instead of swapping the whole string which helps in the efficient use of memory and time.
6. When should we use pointers in a C program?
• To pass a large structure liked server request or response packet.
• To implement the linked list and binary trees.
• To play with GPIO or hardware register.
• To get the address or update value from the function (call by reference)
• To create a dynamic array.
• To create a call back function using the function pointer.
Note: Besides it, lots of places where the need to use the pointer.
7. What is Array in C?
An array is essentially a collection of elements. The data type of all elements must be the same and store at the contiguous memory location. So each array can store only one type of data. At the time of the array declaration, you must specify the type of data with the array name.
In array, we can access the elements using an index in square brackets. The index of the array always starts with 0. It means if you want to get the first element of the array then the index must be 0.
8. What is Pointer in C?
A pointer is similar to a variable but the difference is that pointers are store the address of a location in memory and variable stored the value. In other words, we can say, a pointer is used to reference a location in the memory.
When we have used a pointer to store the address in the memory than using the dereferencing techniques we can also get the value from the address which is stored by the pointer.
9. When is a void pointer used?
A void pointer is used for working with raw memory or for passing a pointer to an unspecified type.
Some C code operates on raw memory. When C was first invented, character pointers (char *) were used for that. Then people started getting confused about when a character pointer was a string, when it was a character array, and when it was raw memory.
For example, strcpy() is used to copy data from one string to another, and strncpy() is used to copy at most a certain length string to another:
char *strcpy( char *str1, const char *str2 );
char *strncpy( char *str1, const char *str2, size_t n );
memcpy() is used to move data from one location to another:
void *memcpy( void *addr1, void *addr2, size_t n );