Assess your knowledge of pointers and arrays in C with this quiz that covers basic pointer usage, array manipulation, pointer arithmetic, and memory access concepts. Whether you're a beginner or brushing up, these questions are crafted to reinforce essential C programming foundations vital for beginners.
Which of the following correctly declares an array of 5 integers in C?
Explanation: The correct way to declare an integer array of 5 elements in C is 'int arr[5];'. The option 'int arr;' declares a single integer, not an array. 'int[5] arr;' is incorrect syntax in C. 'array int arr[5];' uses an invalid keyword, as 'array' is not used in C for declarations.
Which syntax correctly declares a pointer to an integer in C?
Explanation: 'int *ptr;' declares a pointer to an integer by placing the asterisk before the pointer variable name. 'pointer int ptr;' is not valid syntax in C. 'int ptr*;' places the asterisk after the variable name, which is incorrect. 'intu0026 ptr;' is not valid in C; the ampersand indicates a reference in languages like C++, not C.
What will '*(arr + 2)' return if 'arr' is an integer array containing {4, 8, 15, 16}?
Explanation: '*(arr + 2)' accesses the third element (index 2) of the array, which is 15. '8' corresponds to arr[1], not arr[2]. '4' is the first element, arr[0], and '16' is the last element, arr[3]. Pointer arithmetic moves in multiples according to the size of the data type.
What is the value stored in a pointer variable 'p' immediately after the statement 'int *p = NULL;'?
Explanation: Assigning 'NULL' to a pointer sets it to a value representing no valid memory address. The answer '0' is close, as NULL often evaluates to 0, but using 'NULL' is more explicit and proper in pointer contexts. 'Uninitialized value' is incorrect because 'p' is set to NULL. 'The memory address of an integer' is incorrect since no address is assigned.
Which expression gives the address of the first element in an array 'nums'?
Explanation: In C, the name of the array 'nums' is equivalent to the address of its first element. 'u0026nums[0]' also gives the address of the first element, so it is equally correct, but 'nums' matches the standard definition. 'nums[1]' gives the value at the second element, not an address, and '*nums' dereferences the address, yielding the first value.
If 'arr' is an integer array and 'ptr = arr;', what does 'ptr[2] = 7;' do?
Explanation: Since 'ptr' points to 'arr', writing 'ptr[2] = 7;' changes 'arr[2]' to 7. Assigning 7 to 'ptr' itself would require 'ptr = 7;', not 'ptr[2] = 7;'. Changing all elements to 7 would require a loop. Setting the address in 'ptr[2]' to 7 would only apply to arrays of pointers, not integers.
Given 'int values[3] = {10, 20, 30};', what will the expression '*(values + 1)' return?
Explanation: '*(values + 1)' evaluates to the second element of the array, which is 20. '10' is the first element, accessed with '*(values + 0)'. '30' is the third element, '*(values + 2)'. 'values' itself is a pointer to the first element, not a value.
If 'int arr[10];' and 'int *ptr = arr;', what does 'sizeof(arr) / sizeof(arr[0])' compute to?
Explanation: 'sizeof(arr)' gives the total bytes occupied by the array, and 'sizeof(arr[0])' gives the bytes for one integer. The division computes the number of elements, which is 10 in this case. '1' would only be correct for a single-element array. '4' is often the size of one int, not the count. 'Undefined' is incorrect, as this is a common and defined pattern.
Given 'int num = 5; int *p = u0026num;', what does '*p' return?
Explanation: '*p' dereferences the pointer, accessing the value stored where 'p' points, which is 5. 'The value of p' would give the address, not the value. 'u0026num' gives the address of num, while 'An error' is incorrect if the syntax is valid.
What is passed to a function when an array is used as an argument in C, like 'function(arr);'?
Explanation: In C, using an array as a function argument passes the address of its first element, allowing the function to manipulate the original array. 'The entire array' would require passing by value, which C does not do for arrays. 'A copy of the first element' is incorrect, as the whole array can be manipulated. 'Only the last element' is not relevant to how arrays are passed.