C Arrays (1D & 2D) - Tricky MCQ
Tricky Questions on 1D & 2D Arrays
What is the output of: printf("%d", sizeof(arr)/sizeof(arr[0])) when arr is passed to a function?
When an array is passed to a function, it decays to a pointer to its first element. Inside the function, sizeof(arr) gives the size of the pointer (usually 4 or 8 bytes), not the size of the original array. This is a common pitfall when trying to calculate array size inside functions.
Can we use arr[-1] in C arrays?
Negative array indices are syntactically valid in C due to pointer arithmetic. arr[-1] is equivalent to *(arr - 1). However, accessing memory before the array (outside allocated bounds) leads to undefined behavior. It may work in some cases but is dangerous and should be avoided.
What is arr[i] equivalent to in pointer terms?
Due to the commutative property of addition and how array indexing works in C, arr[i] is exactly equivalent to *(arr + i), which is also equivalent to *(i + arr), which further simplifies to i[arr]. All four forms are valid and compile to the same machine code.
What happens when you declare: int arr[0]; ?
In standard C, zero-length arrays are invalid. However, GCC allows them as an extension. When used as the last member of a struct (C99's flexible array member), [] is preferred over [0] or [1]. Zero-length arrays have sizeof equal to 0 and can be useful for variable-length structures.
What is the difference between int arr[] and int *arr?
int arr[] declares an array (complete type), while int *arr declares a pointer. sizeof(arr) gives size of entire array for array declaration, but size of pointer for pointer declaration. Arrays cannot be reassigned (arr = something is invalid), while pointers can. However, in function parameters, they're equivalent due to array decay.
Can we have arrays with runtime size in C?
In C89, array sizes must be compile-time constants. However, you can create dynamic arrays at runtime using malloc(). C99 introduced VLAs (Variable Length Arrays) that allow arrays with runtime-determined size on the stack, but they have limitations (can't be global/static, size can't be changed). VLAs are optional in C11.
What is the memory layout of 2D array int arr[3][4]?
C uses row-major order for multidimensional arrays. int arr[3][4] allocates a single contiguous block of 3×4 = 12 integers. Elements are stored row by row: arr[0][0], arr[0][1], arr[0][2], arr[0][3], arr[1][0], arr[1][1], etc. This is different from an array of pointers (like int* arr[3]) which requires separate allocations for each row.
What does arr represent when declared as int arr[5]?
arr is the array itself, not a pointer. However, in most expressions, array names decay to pointers to their first element (exception: sizeof, &, string literal initialization). So arr usually behaves like &arr[0]. But &arr gives address of entire array (same value but different type: pointer to array vs pointer to element).
What is array-to-pointer decay?
Array-to-pointer decay is an implicit conversion that occurs when an array is used in most expressions. The array name converts to a pointer to its first element. This happens in function calls (parameters), assignment (except initialization), and most operations. Exceptions include sizeof, &, and string literal initialization of char arrays.
Can we compare two arrays using == operator?
When you use == with arrays, array-to-pointer decay occurs, so you're comparing pointers (addresses), not array contents. Two arrays with identical contents will compare as false if they're different objects. To compare array contents, use memcmp() or write a loop to compare element by element.
What is the type of &arr for int arr[5]?
&arr gives the address of the entire array, not just the first element. While it has the same numerical value as &arr[0], it has different type: pointer to array of 5 ints (int(*)[5]) vs pointer to int (int*). This affects pointer arithmetic: &arr + 1 jumps sizeof(arr) bytes, while &arr[0] + 1 jumps sizeof(int) bytes.
Can array size be negative?
For regular arrays, negative size is a compilation error. For VLAs (Variable Length Arrays in C99), if the size expression evaluates to negative at runtime, the behavior is undefined (could crash, produce weird results, etc.). The C standard says VLA size must be > 0. Some compilers may reject negative VLA sizes at compile-time.
What is the maximum size of an array in C?
The maximum array size is constrained by multiple factors: available memory (physical+virtual), SIZE_MAX (maximum value of size_t), implementation limits (stack size for local arrays), and integer overflow. The C standard guarantees arrays up to 65535 bytes, but implementations typically support much larger. Dynamic arrays via malloc() have fewer restrictions than stack arrays.
How are 2D arrays passed to functions?
For 2D arrays, the first dimension can be omitted in function parameters because it decays to pointer, but all other dimensions must be specified for pointer arithmetic to work. int arr[][N] and int (*arr)[N] are equivalent in function parameters. The compiler needs to know N to calculate arr[i][j] = *(arr + i*N + j).
Can we have arrays of void type?
void is an incomplete type that cannot be completed. You cannot declare variables, arrays, or functions returning arrays of void. void has no size, so sizeof(void) is invalid. However, you can have arrays of void* (pointers to void), which are commonly used for generic containers as void* can point to any data type.