Bird
0
0

You want to create a constant array of 3 integers in C that cannot be modified after initialization. Which of the following declarations achieves this correctly?

hard📝 Application Q15 of 15
C - Variables and Data Types
You want to create a constant array of 3 integers in C that cannot be modified after initialization. Which of the following declarations achieves this correctly?
Aint arr[3] = {1, 2, 3};
Bint const *arr = {1, 2, 3};
C#define arr {1, 2, 3}
Dconst int arr[3] = {1, 2, 3};
Step-by-Step Solution
Solution:
  1. Step 1: Understand const array declaration

    Declaring an array with const int arr[3] means the elements cannot be changed after initialization.
  2. Step 2: Check each option

    int arr[3] = {1, 2, 3}; declares a normal array, elements can be changed. const int arr[3] = {1, 2, 3}; declares a constant array correctly. #define arr {1, 2, 3} uses #define incorrectly for array. int const *arr = {1, 2, 3}; declares a pointer to const int, but initialization is invalid.
  3. Final Answer:

    const int arr[3] = {1, 2, 3}; -> Option D
  4. Quick Check:

    const array elements cannot change [OK]
Quick Trick: Use const before type for constant arrays [OK]
Common Mistakes:
  • Using #define for arrays
  • Declaring non-const arrays
  • Incorrect pointer initialization

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Quizzes