Understanding Arrays in C: A Beginner’s Guide

An array is a series of homogeneous pieces of data that are all identical in type, but the type can be quite complex. An array is a very popular data type with C programmers. This is because of the convenience with which arrays lend themselves to programming. A string is simply a special case of an array.

An array is a collective name given to a group of ‘similar quantities’. These similar quantities could be percentage marks of 100 students, or salaries of 300 employees, or ages of 50 employees. What is important is that the quantities must be ‘similar’. Each member in the group is referred to by their position in the group.

Declaring the array:  We can declare an array by specifying its data type, name, and the number of elements the array holds between square brackets immediately following the array name.

data type array_name[size];

For example:

int marks[10]; 

in this declaration the type of array is int, its name is marks and size is 10. It means the marks array can contain 10 elements.

Rules of Array

  • The data type can be any valid C data type including structure and union.
  • The array name has to follow the rule of variable and the size of the array has to be a positive constant integer.
  • We can access array elements via indexes array_name[index].
  • Indexes of an array start from 0, not 1 so the highest element of an array is array_name[size-1]. For example in array marks[10], its indexing will start from marks[0] and will end at marks[9].

Initializing the array: It is like a variable, an array can be initialized. To initialize an array, you provide initializing values which are enclosed within curly braces in the declaration and placed following an equals sign after the array name.

int list[5] = {2,3,4,5,6};
OR 
int list[] = {2,1,3,7,8};

Note:

  1. Till the array elements are not given any specific values, they are supposed to contain garbage values.
  2. If the array is initailized where it is declared mentioning the dimension of the array is optional.

Array of Pointers

An array of pointers is a collection of addresses. For example, suppose we have a declare a pointer that can hold the addresses of 3 variables at a time. To achieve this we need to write the following:

Int * P[3];    //This statement will create a pointer that can hold the address of 3 integral variables.

Let assume we have three variables

int a=10, b=20,c=30; 

Then we can assign the address of a,b,c to pointer P by the following statement.

P[0]= &a;
P[1]= &b;
P[2] =&c;

For  accessing  the values of a,b, and c through pointer P we write

*(P[0])
*(P[1])
*(P[2])

Programs:-

Program for passing an entire array to a function using pointer:-

#include <stdio.h> 

void main() 
{  
int list[5] = {2,1,3,7,8};  
int i;
printf(“your array elements are:”);
for(i=0;i<5;i++)
{
	Printf(“\n%d”,list[i]);
}

Function(&list[0]); // this is function calling by passing the base address of array.

Getch();
}

Function(int *plist)
{
Int i;
 // accessing array elements using pointer 
 for(i = 0; i < 5;i++) 
 { 
 printf("%d\n",*plist);  
 plist++; 
 }
 }

Program to implement Pointers and Arrays:-

To be able to see what pointers have got to do with arrays, let us first learn some pointer arithmetic. Consider the following example:

main( ) 
{
int i = 3, *x ; 
float j = 1.5, *y ; 
char k = 'c', *z ; 
printf ( "\nValue of i = %d", i ) ; 
printf ( "\nValue of j = %f", j ) ; 
printf ( "\nValue of k = %c", k ) ;
 x = &i ;
 y = &j ; 
z = &k ; 
printf ( "\nOriginal address in x = %u", x ) ; 
printf ( "\nOriginal address in y = %u", y ) ; 
printf ( "\nOriginal address in z = %u", z ) ; 
x++ ;
 y++ ;
 z++ ; 
printf ( "\nNew address in x = %u", x ) ; 
printf ( "\nNew address in y = %u", y ) ; 
printf ( "\nNew address in z = %u", z ) ; 
} 

Here is the output of the program.

Value of i = 3 
Value of j = 1.500000 
Value of k = c 
Original address in x = 65524 
Original address in y = 65520 
Original address in z = 65519 
New address in x = 65526 
New address in y = 65524 
New address in z = 65520 

Observe the last three lines of the output. 65526 is the original value in x plus 2, 65524 is the original value in y plus 4, and 65520 is the original value in z plus 1. This so happens because every time a pointer is incremented it points to the immediately next location of its type. That is why, when the integer pointer x is incremented, it points to an address two locations after the current location, since int is always 2 bytes long. Similarly, y points to an address 4 locations after the current location and z points 1 location after the current location. This is a very important result and can be effectively used while passing the entire array to a function.