Notifications
Clear all

[Solved] How do you initialize an array in C?

0
Topic starter

A. int arr [3] = (1,2,3);
B. int arr (3) = {1,2,3};
C. int arr [3] = {1,2,3};
D. int arr (3) = (1,2,3);
View Answer

C. int arr [3] = {1,2,3};

 

Explanation: 

 

The Basics of Initializing an Array in C

Arrays are variables that store multiple data values of the same type in one place. In C, arrays are contiguous blocks of memory locations; you can think of them as being similar to an array of boxes all lined up in the same area. The number of elements contained in an array is fixed when the array is created, and cannot be changed afterward without creating a new array and copying all the values from the old array into the new one.

Step 1: Declare the Array
All arrays must be declared with a specific type. This declaration can occur either before or after the rest of the code for your program, so you have some freedom as to where to put it. Here is an example declaration of a two-dimensional array that holds two strings:
int* data = NULL;

Step 2: Assign Values to the Array
We then need to assign values to each element, so that the corresponding index has a value. The syntax for this is as follows:

int arr [3] = {1,2,3};

Step 3: Access the Elements
When the first element is accessed, it is stored at the memory address x. The second element would be stored at x+1, and so on for all subsequent elements.
This means that if you want to initialize an array at address 1234 with the value 1, your code would be: int a 

iubians