Arrays in C

What is array?

  1. An array is the collection of similar type of elements stored in contiguous memory locations.
  2. All array elements share the common name, but each element is uniquely identified by their index or subscript. so the array is also known as a subscript variable.
  3. Array index always starts with 0(zero).

Continuous Storage

  1. As shown in the above figure all array elements are stored in a contiguous memory location.
  2. The above array is an integer array and if an integer occupy 4 bytes in memory and first element stored at memory address 100 then the next element addresses are 104,108,112,116 respectively.

Creating an array

int arr[5];
  1. While creating an array size of an array is also specified by using the subscript([]) operator.
  2. The size of an array must be integer constant or integer constant expression.
  3. A variable or variable expression is not allowed as an array size.
  4. Floating-point constants and expressions are not allowed as array size.

Syntax of array declaration

datatype array_variable[array_size];

The following few examples show some valid/invalid declaration of array...

Array Declaration examples

int arr[5]; //valid
int arr[5 + 4]; //valid
float arr[10]; //valid
int arr[5.4]; //invalid, floating-point constant not allowed as an array size.

int x = 10;
int arr[x]; //invalid, variable is not allowed as an array size.

Size of array

int iarr[10]; // 4 * 10 = 40 bytes
float farr[10]; // 4 * 10 = 40 bytes
char carr[10]; // 1 * 10 = 10 bytes

If an integer occupies 4 bytes then the code snippet integer array(iarr) declaration occupies 4 * 10 = 40 bytes in memory.

Similarly, float occupies 4 bytes so farr occupies 40 bytes.

A char occupies 1 byte so carr occupies 10 bytes.

General Formula to calculate array size

memory_size_of_data_type * size_of_array

Array initialization

Similar to the other data types array elements can also initialize at the time or array declaration.

The following code snippet shows, How to initialize array elements.

int arr[5];//All array elements are initialized with garbage values.
int arr[5] = {1,2,3,4,5};
//Array elements can be initialized with comma-seperated list of values.
int arr[5] = {1,2}; // {1,2,0,0,0}
//If array is partially initialized, the rest of the array elements are initialized with zero.
int arr[] = {1,2,3};
//If initialize array, size of array is optioanl it can be dynamically received from the intilized group. 

Accessing array elements

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

To access array elements subscript([]) operator is used.

Array elements can be accessed by using their index.

Access individual array elements

printf("%d",arr[0] + arr[3]);

As shown in the above code snippet, individual array elements access by using their index. As shown in the above code snippet we access the 1st and 4th element by using arr[0] and arr[3] respectively and find the sum of both elements.

Iterate array elements using a loop

for(i = 0; i < 5; i++) {
    printf("%d",arr[i]);
}

If we want to access all array elements then it's better to iterate array using a loop.

  1. As shown in the above code snippet, we start the loop from 0(zero) because the array index starts with 0.
  2. we run the loop from 0 to 4(<5) because if an array is of 5 elements then index of the last element will be 4(5-1).

Input array elements

int arr[5];
int i;
for(i = 0; i < 5; i++) {
	printf("Enter element %d: ",i+1);
	scanf("%d",&arr[i]);
}

As shown in the above code snippet, that before receiving input of each element we print a message that prompts the user to enter element for a position and position starts with 1, not 0. This is because a normal user doesn't know that the array index starts with 0.

As we know that array size is fixed(constant) so we create the array of enough size and use the elements till input size as shown in the following code snippet.

int arr[10];
int n,i;
printf("Enter the size of array want to use,<max:10>: ");
scanf("%d",&n);
	
for(i = 0; i < n; i++) {
	printf("Enter element %d: ",i+1);
	scanf("%d",&arr[i]);
}

Program to find the average of array elements

#include<stdio.h>
int main() {
	int arr[10];
	int i,n;
	int sum;
	float avg;
	
	printf("Enter the size of array want to use,: ");
	scanf("%d",&n);
	
	sum = 0;
	for(i = 0; i < n; i++) {
		printf("Enter element %d: ",i+1);
		scanf("%d",&arr[i]);
		
		sum += arr[i];
	}	
	
	avg = (float)sum / n;
	
	printf("Sum = %d\nAvg = %.2f",sum,avg);
	return 0;
}
Enter the size of array want to use,<max:10>: 5
Enter element 1: 2
Enter element 2: 6
Enter element 3: 4
Enter element 4: 3
Enter element 5: 7
Sum = 22
Avg = 4.40

Limitations of array

  1. Array size is fixed, so always there is a chance of shortage and wastage of memory space.
  2. Insertion and deletion need shifting of the array element that is a time-consuming operation for long arrays.