C - Arrays



Array is a fundamental data structure in C. An array in C is a collection of data items of similar data type. One or more values same data type, which may be primary data types (int, float, char), or user−defined types such as struct, or pointers can be stored in an array. In C, the type of elements in the array should match with the data type of the array itself. The size of the array, also called the length of the array, must be specified in the declaration itself. Once declared, the size of a C array cannot be changed. When an array is declared, the compiler allocates a continuous block of memory required to store the declared number of elements.

Imagine we want to store marks of 10 students and find the average. We declare 10 different variables to store 10 different values as follows −

int a = 50, b = 55, c = 67, . . . .;
float avg = (float)(a + b + c +. . . )/10;

These variables will be scattered in the memory with no relation between them. Importantly, if we want to extend the problem of finding the average oof marks of 100 (or more) students, declaring so many individual variables becomes impractical.

Array offers a compact and memory−efficient solution. Because the elements in an array are stored in adjacent location, we can easily access any element in relation to the current element. As each element has an index, it can be directly manipulated.

To go back to the problem of storing marks of 10 students and find average, the solution with the use of array would be −

int marks[10] = {50, 55, 67, 73, 45, 21, 39, 70, 49, 51};
int i, sum=0;
float avg;
for (i=0; i<=9; i++){
   sum+ = marks[i];
}

avg = (float)sum/10;

Array elements are stored in contiguous memory locations. Each element is identified by an index starting with 0. The lowest address corresponds to the first element and the highest address to the last element.

Arrays

Declaring Arrays

To declare an array in C, you need to specify the type of the elements and the number of elements to be stored in it

type arrayName [ Size ];

The Size must be an integer constant greater than zero and type can be any valid C data type. The size is put inside the square brackets.

There are different ways in which an array is declared in C.

Declaring uninitialized array

int a[5];

In such type of declaration, the uninitialized elements in the array may show certain random garbage values.

Example

#include <stdio.h>

int  main() {
   int arr[5];
   int i;

   for (i=0; i<=4; i++){
      printf("a[%d]: %d\n", i, arr[i]);
   }
   return 0;
}
Output
a[0]: -133071639
a[1]: 32767
a[2]: 100
a[3]: 0
a[4]: 4096

Initialize array

If a comma−separated sequence values put inside curly brackets is assigned in the declaration, the array is created with each element initialized with corresponding value.

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

Initialize all elements to 0

To initialize all elements to 0, put it inside curly brackets

Example

#include <stdio.h>
int  main() {
   int arr[5] = {0};
   int i;

   for (i=0; i<=4; i++){
      printf("a[%d]: %d\n", i, arr[i]);
   }
   return 0;
}
Output
a[0]: 0
a[1]: 0
a[2]: 0
a[3]: 0
a[4]: 0

If the list of values is less than the size of the array, the rest of the elements are initialized with 0.

Example

#include <stdio.h>
int  main() {
   int arr[5] = {1,2};
   int i;

   for (i=0; i<=4; i++){
      printf("a[%d]: %d\n", i, arr[i]);
   }
   return 0;
}
Output
[0]: 1
a[1]: 2
a[2]: 0
a[3]: 0
a[4]: 0

When an array may be partially initialized, you can specify the element in the square brackets.

Example

#include <stdio.h>
int  main() {
   int a[5] = {1,2, [4] = 4};
   int i;
   for (i=0; i<=4; i++){
      printf("a[%d]: %d\n", i, a[i]);
   }
   return 0;
}
Output
a[0]: 1
a[1]: 2
a[2]: 0
a[3]: 0
a[4]: 4

Array size

The compiler allocates a continuous block of memory. The size of the memory allocates depends on the data type of the array. If an int array of 5 elements is declared, the array size in number of bytes is the sizeof(int)*5

Example

#include <stdio.h>
int  main() {
   int arr[5] = {1,2, 3, 4, 5};
   printf("size of array: %ld", sizeof(arr));
   return 0;
}

Output

size of array: 20

The sizeof operator returns the number of bytes occupied by the variable. The size of each int is 4 bytes.

The compiler allocates each element in adjacent locations

Example

#include <stdio.h>

int  main() {
   int a[] = {1,2,3,4,5};
   int i;

   for (i=0; i<4; i++){
      printf("a[%d]: %d address: %d\n", i, a[i], &a[i]);
   }
   return 0;
}

Output

a[0]: 1 address: 6422016
a[1]: 2 address: 6422020
a[2]: 3 address: 6422024
a[3]: 4 address: 6422028

In this array, each element is of int type. Hence, the 0th element occupies first 4 bytes 642016 to 19. The element at next subscript occupies next four bytes and so on.

If we have the array type of double type, element at each subscript occupies eight bytes

Example

#include <stdio.h>
int  main() {
   double a[] = {1.1,2.2, 3.3,4.4,5.5};
   int i;

   for (i=0; i<4; i++){
      printf("a[%d]: %f address: %ld\n", i, a[i], &a[i]);
   }
   return 0;
}

Output

a[0]: 1.100000 address: 1699042976
a[1]: 2.200000 address: 1699042984
a[2]: 3.300000 address: 1699042992
a[3]: 4.400000 address: 1699043000

The length of a char variable is one byte. Hence a char array length will be equal to the array size.

Example

#include <stdio.h>
int  main() {
   char a[] = "Hello";
   int i;

   for (i=0; i<5; i++){
      printf("a[%d]: %c address: %ld\n", i, a[i], &a[i]);
   }
   return 0;
}

Output

a[0]: H address: 6422038
a[1]: e address: 6422039
a[2]: l address: 6422040
a[3]: l address: 6422041
a[4]: o address: 6422042

Accessing Array Elements

Each element in the array is identified by a unique incrementing index stating with 0. To access the element by its index, this is done by placing the index of the element within square brackets after the name of the array. Elements of an array are accessed by specifying the index ( offset ) of the desired element within square [ ] brackets after the array name.

For example −

double salary = balance[9];

The above statement will take the 10th element from the array and assign the value to salary variable. The following example Shows how to use all the three above mentioned concepts viz. declaration, assignment, and accessing arrays.

Example

#include <stdio.h>
 
int main () {

   int n[ 10 ]; /* n is an array of 10 integers */
   int i,j;
 
   /* initialize elements of array n to 0 */         
   for ( i = 0; i < 10; i++ ) {
      n[ i ] = i + 100; /* set element at location i to i + 100 */
   }
   
   /* output each array element's value */
   for (j = 0; j < 10; j++ ) {
      printf("n[%d] = %d\n", j, n[j] );
   }
 
   return 0;
}

Output

n[0] = 100
n[1] = 101
n[2] = 102
n[3] = 103
n[4] = 104
n[5] = 105
n[6] = 106
n[7] = 107
n[8] = 108
n[9] = 109

The index gives random access to the array elements. An array may consist of struct variables, pointers and even other arrays as its elements.

Advertisements