Arrays in C
Arrays in C
• What is Array?
• Representation of an Array
• Declaration Syntax of Array
• Initialization of an Array
• Types of Arrays:
• One -Dimensional Arrays
• Multi-Dimensional Arrays
• Advantages of Arrays
• Disadvantages of Arrays
• Video on Arrays
• FAQs Related to Arrays
What is Array?
An array is a group of similar elements or data items of the same type collected at
contiguous memory locations. In simple words, we can say that in computer
programming, arrays are generally used to organize the same type of data.
Arrays always store the same type of values. In the above example:
Important: Array can store only the same type of data items. From the below example
you can see how it works:
• In the array a, we have stored all integral values (same type)
• In the array b, we have stored all char values (same type)
• In the array c, there is integral, float, char all types of values and this is not something an
array can store so, option 3 is wrong because an array cannot store different types of values.
int A[10];
2 5 8 44 21 11 7 9 3 1
char B[10];
f d a b n j l s e y
Initialization of an Array:
If an array is described inside a function, the elements will have garbage value. And in
case an array is static or global, its elements will be initialized automatically to 0.
We can say that we can simply initialize elements of an array at the time of declaration
and for that, we have to use the proper syntax:
Types of Arrays:
There are two types of arrays:
• One-Dimensional Arrays
• Multi-Dimensional Arrays
Multi-Dimensional Arrays
In multi-dimensional arrays, we have two categories:
• Two-Dimensional Arrays
• Three-Dimensional Arrays
1.
1. Two-Dimensional Arrays
An array involving two subscripts [] [] is known as a two-dimensional array. They are
also known as the array of the array. Two-dimensional arrays are divided into rows and
columns and are able to handle the data of the table.
When we require to create two or more tables of the elements to declare the array
elements, then in such a situation we use three-dimensional arrays.
Advantages of Array
• It is a better version of storing the data of the same size and same type.
• It enables us to collect the number of elements in it.
• Arrays have a safer cache positioning that improves performance.
• Arrays can represent multiple data items of the same type using a single name.
Disadvantages Of Array:
• In an array, it is essential to identify the number of elements to be stored.
• It is a static structure. It means that in an array, the memory size is fixed.
• When it comes to insertion and deletion, it is a bit difficult because the elements are stored
sequentially and the shifting operation is expensive.
A. An array of 50 numbers
B. An array of 100 numbers
C. An array of 500 numbers
D. A dynamically allocated array of 550 numbers
Q. The following C functions in which size is the number of elements in the array E:
int Y = 0;
int Z;
int i,j,k;
Y = Y + E[i];
Z = 0;
Z = Z + E[k];
if(Z > Y)
Y = X;
return Y;
Types of arrays?
There are two types of array:
• Two-dimensional array.
• Multi-dimensional array
Q3