C Language Interview Questions: High-Frequency Issues with Pointers and Arrays

C Language Interview Questions: High-Frequency Issues with Pointers and Arrays

In the study and application of the C language, pointers and arrays are two extremely important concepts. These two are not only closely related but are also often key points of examination in interviews. This article will help you better understand pointers and arrays through several high-frequency interview questions.

1. Basic Concepts

1. Arrays

An array is a data structure that can store a fixed number of elements of the same type. The size of the array must be specified at the time of declaration.

int arr[5]; // Declare an array containing 5 integers

2. Pointers

A pointer is a data type used to store the address of a variable. For any variable, a pointer of the same type can be used to reference it.

int a = 10; int *p = &a; // p is the address of a, meaning p points to a

After understanding these basic concepts, let’s look at some specific questions.

2. High-Frequency Questions and Analysis

Question 1: How to define an integer array and access its elements using pointers?

Code Example:

#include <stdio.h>
int main() {    int arr[5] = {10, 20, 30, 40, 50};    int *p = arr; // Point to the first element
    for (int i = 0; i < 5; i++) {        printf("arr[%d] = %d\n", i, *(p + i)); // Access elements using pointer    }
    return 0;}

Analysis: In this example, we first define an integer array <span>arr</span>, then we create an integer pointer <span>p</span> and make it point to <span>arr</span>. The name of the array itself represents the address of the first element of this array. Therefore, by using the dereference operator (*), combined with the offset i, we can access each element.

Question 2: What is the difference between using character arrays and pointers when passing parameters to functions?

Code Example:

#include <stdio.h>
void modifyArray(int arr[]) {    arr[0] = 100; // Modify the first element}
void modifyPointer(int *ptr) {    ptr[0] = 200; // Modify the first element }
int main() {    int array[] = {1, 2, 3};
    modifyArray(array); // Call function using array as argument.
    printf("First element after modifyArray: %d\n", array[0]);
    modifyPointer(array); // Call function using array as argument.
    printf("First element after modifyPointer: %d\n", array[0]);
    return 0;}

Analysis: In C, when we pass an array to a function, we are actually passing the address of the first element of that array. In any case, regardless of the method used (either using <span>arr[]</span> or <span>*ptr</span>), the data at the same memory location is modified. Therefore, both methods have the same effect, but there are slight differences in usage, such as in parameter declaration.

Question 3: What is incorrect out-of-bounds access? What can happen?

Code Example:

#include <stdio.h>
int main() {   int arr[3] = {1, 2, 3};
   for (int i = -1; i <= 3; i++) {       printf("arr[%d] = %d\n", i, arr[i]);    }
   return 0;}

Analysis: The above code attempts to read data from index -1 to index 3, which is incorrect and results in undefined behavior. “Out-of-bounds” means exceeding the legal range, and this action may lead to program exceptions or retrieving random values. The actual behavior depends on the compiler’s strategy, which may also re-enter to save buffer space, so out-of-bounds operations should be avoided as they are incorrect and dangerous practices.

3. Conclusion

This article discussed some high-frequency interview questions regarding “pointers and arrays” in the C language, including how to define, how to call, and precautions. In actual programming, effectively utilizing these two can improve program execution efficiency and reduce memory usage. However, caution must also be exercised to prevent undefined behavior and difficult-to-debug issues. I hope this article helps you gain a deeper understanding of these fundamental concepts, laying a solid foundation for future work and interviews.

Leave a Comment