C Programming Tricky Questions
Tricky Questions on 1D and 2D Arrays
What is array decay and why is it important?
Array decay is when an array name is converted to a pointer to its first element when passed to a function. This means the function receives a pointer, not the actual array, so sizeof() inside the function gives pointer size, not array size.
Tricky Point: This is why you often need to pass array size as a separate parameter to functions.
Can you initialize only specific elements of an array?
Yes, using designated initializers. You can specify which indices to initialize, and all others are automatically set to zero. This works for both 1D and 2D arrays.
What is the difference between array[5] and 5[array]?
They are equivalent due to how array indexing works in C. The expression a[i] is evaluated as *(a + i), and since addition is commutative, *(i + a) is the same, so i[a] works too. However, 5[array] is confusing and should never be used in real code.
How are 2D arrays stored in memory?
2D arrays are stored in row-major order - all elements of row 0 first, then row 1, etc. This is important for cache efficiency and pointer arithmetic.
What happens when you access array[-1]?
It accesses memory before the array start, which is undefined behavior. It might crash, return garbage, or seem to work but corrupt data. This is a common buffer underflow error.
Warning: Negative array indices can lead to security vulnerabilities like buffer overreads.
Can you create variable-length arrays in C?
Yes, since C99, you can create arrays with runtime-determined sizes, called Variable Length Arrays (VLAs). However, they have limitations and are optional in C11.
What is the difference between int arr[ ] and int *arr?
int arr[ ] declares an array (fixed size, memory allocated). int *arr declares a pointer to int (can point to array, malloc memory, or single variable). Arrays are not pointers, though they can decay to pointers.
How do you pass a 2D array to a function?
You must specify at least the second dimension (number of columns). The function prototype needs the column size, like func(int arr[ ][COLS], int rows). The compiler needs column size for pointer arithmetic.
What is array bound checking and does C perform it?
Array bound checking verifies that array indices are within valid range. C does NOT perform automatic bounds checking - it's the programmer's responsibility. This is why buffer overflows are common in C.
Can arrays be compared using == operator?
No, you cannot compare arrays directly with ==. The array names decay to pointers, so you'd be comparing addresses, not contents. Use memcmp() or loop through elements.
What is a jagged array and can you create it in C?
A jagged array is an array of arrays where each row can have different lengths. In C, you can simulate it using an array of pointers, where each pointer points to a 1D array of different size.
What is the relationship between arrays and pointers?
Arrays are not pointers, but array names decay to pointers in most contexts. sizeof() behaves differently: sizeof(array) gives total size, sizeof(pointer) gives pointer size. Arrays cannot be reassigned like pointers.
Can you return an array from a function?
Not directly. You can return a pointer to an array, but the array must not be local to the function (or it'll be destroyed). Options: return pointer to static array, dynamically allocated array, or array passed as parameter.
What is the maximum size of an array in C?
Limited by available memory and implementation constraints. For static arrays, there may be stack size limits. For dynamic arrays (malloc), limited by heap size. Very large arrays may cause segmentation faults.
How does pointer arithmetic work with 2D arrays?
For 2D array arr[M][N], arr[i][j] is equivalent to *(*(arr + i) + j). The compiler needs column size N to calculate offsets correctly: arr + i moves i×N×element_size bytes.
What is the difference between int arr[5] and int arr[5] = {0}?
int arr[5] creates uninitialized array with garbage values (if local). int arr[5] = {0} initializes all elements to zero. Even {0} with partial initialization sets all unspecified elements to zero.
Can array size be zero in C?
No, zero-length arrays are not allowed in standard C, though some compilers support them as an extension. However, flexible array members (arrays with unspecified size at end of struct) are allowed in C99.
What happens when you increment an array name?
You cannot increment an array name directly because array names are not modifiable lvalues. However, you can increment a pointer that points to the array.
What is a flexible array member?
A flexible array member is an array without specified size at the end of a structure. It allows structures to contain variable-length arrays. Memory must be allocated dynamically with enough space for the array.
How do you find the number of elements in an array?
Use sizeof(array) / sizeof(array[0]). This works only for actual arrays, not pointers that have decayed from arrays. Inside functions where array has decayed to pointer, this gives wrong result.
What is the difference between row-major and column-major order?
Row-major stores rows consecutively (C default). Column-major stores columns consecutively (Fortran default). This affects performance - accessing elements in storage order is faster due to cache locality.
Can arrays have negative indices?
Not directly, but if you have a pointer p pointing somewhere inside an array, you can use negative offsets like p[-1] to access previous elements. This is valid as long as you stay within array bounds.
What is array padding and alignment?
Arrays of structures or complex types may have padding between elements for alignment. sizeof() includes this padding. This can affect memory usage and pointer arithmetic.
How are character arrays different from other arrays?
Character arrays used as strings have a null terminator '\0' at end. String functions expect this. Regular character arrays don't need null terminator unless used as strings.
What is the time complexity of accessing array elements?
Array access is O(1) constant time for both reading and writing, assuming index calculation. This is because arrays provide direct memory access via base address + index × element_size.
Tricky Point: While access is O(1), cache effects mean sequential access is much faster than random access due to spatial locality.
Note: These tricky questions cover important concepts about 1D and 2D arrays in C, including memory layout, pointer relationships, common pitfalls, and performance considerations. Understanding these nuances is crucial for writing correct and efficient array code.