Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Arrays in C

An array in C or C++ is a collection of items stored at contiguous memory locations and


elements can be accessed randomly using indices of an array. They are used to store
similar type of elements as in the data type must be the same for all elements. They can
be used to store collection of primitive data types such as int, float, double, char, etc of
any particular type. To add to it, an array in C or C++ can store derived data types such
as the structures, pointers etc. Given below is the picturesque representation of an
array.

➢ Why do we need arrays?


We can use normal variables (v1, v2, v3, ..) when we have a small number of objects,
but if we want to store a large number of instances, it becomes difficult to manage them
with normal variables. The idea of an array is to represent many instances in one
variable.
➢ Advantages of an Array in C/C++:
1. Random access of elements using array index.
2. Use of less line of code as it creates a single array of multiple elements.
3. Easy access to all the elements.
4. Traversal through the array becomes easy using a single loop.
5. Sorting becomes easy as it can be accomplished by writing less line of
code.
➢ Disadvantages of an Array in C/C++:
1. Allows a fixed number of elements to be entered which is decided at the
time of declaration. Unlike a linked list, an array in C is not dynamic.
2. Insertion and deletion of elements can be costly since the elements are
needed to be managed in accordance with the new memory allocation.
Declaring Arrays:
To declare an array in C, a programmer specifies the type of the elements and the
number of elements required by an array as follows –
Syntax:
type arrayName [arraySize];
This is called a single-dimensional array. The arraySize must be an integer constant
greater than zero and type can be any valid C data type. For example, to declare a 10-
element array called balance of type double, use this statement −
double balance[10];
Here balance is a variable array which is sufficient to hold up to 10 double numbers.

Initializing Arrays:
It is possible to initialize an array during declaration. For example,

int mark[5] = {19, 10, 8, 17, 9};

The number of values between braces { } cannot be larger than the number of elements
that we declare for the array between square brackets [ ].
You can also initialize an array like this.

int mark[] = {19, 10, 8, 17, 9};

Here, we haven't specified the size. However, the compiler knows its size is 5 as we are
initializing it with 5 elements.

Here,

mark[0] is equal to 19 mark[3] is equal to 17

mark[1] is equal to 10 mark[4] is equal to 9

mark[2] is equal to 8
Input and Output Array Elements:

Here's how you can take input from the user and store it in an array element.

// take input and store it in the 3rd element


scanf("%d", &mark[2]);

// take input and store it in the ith element


scanf("%d", &mark[i-1]);

Here's how you can print an individual element of an array.

// print the first element of the array


printf("%d", mark[0]);

// print the third element of the array


printf("%d", mark[2]);

// print ith element of the array


printf("%d", mark[i-1]);

You might also like