10 C Interview Questions and Answers on Pointers
Pointers are variables that are used to store addresses. The concept of pointer is considered to be one of the difficult part of learning the C and C++ programming languages. There are several easy ways to write programs without pointers, but in case of dynamic memory allocation, the knowledge of pointers is a must.
Knowing about memory locations and addresses defined will enable you with the ideas of how every variable function in a program.
So, if you’re a programmer and applying for a job as a developer, here are 10 C interview questions on pointers that you need to go through.
1. What does it mean when you use a pointer with if statement?
Answer: If you use a pointer with a conditional statement it might mean-
- the two pointers maybe same
- if the pointer is indicating a specific value, like if ((*p)==10);
- if the pointer is char*, you can have more functions related to strings
Many programmers often get confused about pointers used in conditional statements as they only think that the pointer is non-null in such case.
2. How are pointers different than array?
Answer: Pointers are nothing but a part of programming that manipulate data with their address. They use * operator to access the data from a particular address.
Arrays are group of memory cells that store data. They use sub-scripted variables in order to access and manipulate the data.
3. How can you combine the following two statements in C to one?
p = (char*) malloc(100);
Answer: These two statements related to pointers can be combined to one single statement as-
char *p = (char*)malloc(100);
4. What do you understand by normalization of pointers?
Answer: Normalization is the process by which an address is converted to a form such that if two non-normalized pointers point to the same address, they both are converted to normalized form, thereby having a specific address.
5. Are there any demerits of using pointer?
Answer: Yes. As pointers have access to a particular memory location, the security level decreases and restricted memory areas can be accessed. Other demerits include memory holes, process and memory panics, etc.
6. Can pointers be added?
Answer: In general, pointer addition is not taken into consideration because the addition of two pointers might exceed the memory size. The only time pointers can be added is when the memory size after addition does not exceed the total memory allocated/available.
7. When can you use a pointer with a function?
Answer: A pointer can be used with a function-
- When an address is to be passed to a function
- When an array elements are to be accessed through a function. Passing base address will give access to the whole array.
8. When can a far pointer be used?
Answer: Sometimes the task we are required to do might not fit in the allocated data and code segments. Far pointers help to access rest of the memory inside a program. Far pointers are the information present outside the data segment (generally 64 kb). Such pointers are used when we need to access address outside of the current segment.
9. Are there any pointers that don’t point to a valid object?
Answer: Yes. There are dangling and wild pointers in programming that don’t point to a valid object. If a data is deallocated from a memory location, the dangling pointer stills points to the empty location which is not required or used.
10. What is a void pointer?
Answer: Void pointer is a generic pointer in programming. If the pointer type is not known, we make use of the void pointer.
In general, the concept of pointer seems to be very difficult as you need to learn about memory allocation, different data types and relevant types of pointers that can be used in the program. In such case, make sure that you’ve already read the C interview questions on data types and make sure that you understand them.
If you’ve any questions regarding pointers in C programming, or if you have C interview questions on pointers that you could not answer, write us in the comments below!