Arrays
An array is a data structure that contains a group of
elements. Typically these elements are all of the same
data type, such as an integer or string.
Arrays are commonly used in computer programs to
organize data so that a related set of values can be
easily sorted or searched.
Single-Dimensional Arrays
Generic declaration:
typename variablename[size]
– typename is any type
– variablename is any legal variable name
– size is a number the compiler can figure out
– For example
int a[10];
– Defines an array of ints with subscripts ranging from 0 to 9
– There are 10*sizeof(int) bytes of memory reserved for this
array.
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
– You can use a[0]=10; x=a[2]; a[3]=a[2]; etc.
– You can use scanf("%d",&a[3]);
Using Constants to Define Arrays
It is useful to define arrays using constants:
#define MONTHS 12
int array [MONTHS];
However, in ANSI C, you cannot
int n;
scanf(“%d”, &n);
int array[n];
In GNU C, the variable length array is allowed.
In ANSI C, the handling of variable length array is more
complicated.
Array-Bounds Checking
C, unlike many languages, does NOT check array
bounds subscripts during:
– Compilation (some C compilers will check literals)
– Runtime (bounds are never checked)
If you access off the ends of any array, it will calculate
the address it expects the data to be at, and then
attempts to use it anyways
– may get “something…”
– may get a memory exception (segmentation fault, bus error,
core dump error)
It is the programmer’s responsibility to ensure that their
programs are correctly written and debugged!
– This does have some advantages but it does give you all the
rope you need to hang yourself!
Initializing Arrays
Initialization of arrays can be done by a comma
separated list following its definition.
For example:
int array [4] = { 100, 200, 300, 400 };
– This is equivalent to:
int array [4];
array[0] = 100;
array[1] = 200;
array[2] = 300;
array[3] = 400;
You can also let the compiler figure out the array size
for you:
int array[] = { 100, 200, 300, 400};
A Simple Example
#include <stdio.h>
void main()
{ int arr[10];
int i;
printf("\n\nRead and Print elements of an array:\n");
printf("-----------------------------------------\n");
printf("Input 10 elements in the array :\n");
for(i=0; i<10; i++) {
printf("element - %d : ",i);
scanf("%d", &arr[i]); }
printf("\nElements in array are: ");
for(i=0; i<10; i++) {
printf("%d ", arr[i]);
}
printf("\n"); }
Multidimensional Arrays
Arrays in C can have virtually as many dimensions as
you want.
Definition is accomplished by adding additional
subscripts when it is defined.
For example:
– int a [4] [3] ;
defines a two dimensional array
a is an array of int[3];
In memory:
a[0][0] a[0][1] a[0][2]
a[0] a[1] a[2] a[3]
Initializing Multidimensional Arrays
The following initializes a[4][3]:
int a[4] [3] = { {1, 2, 3} , { 4, 5, 6} , {7, 8, 9} , {10, 11, 12} };
Also can be done by:
int a[4] [3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
– is equivalent to
a[0][0] = 1;
a[0][1] = 2;
a[0][2] = 3;
a[1][0] = 4;
...
a[3][2] = 12;
An Example
#include<stdio.h>
int main()
{ /* 2D array declaration*/
int disp[2][3]; /*Counter variables for the loop*/
int i, j;
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("Enter value for disp[%d][%d]:", i, j);
scanf("%d", &disp[i][j]); } } //Displaying array elements
printf("Two Dimensional array elements:\n");
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("%d ", disp[i][j]);
if(j==2){
printf("\n"); } } }
return 0;
}
Output:
Enter value for disp[0][0]:1
Enter value for disp[0][1]:2
Enter value for disp[0][2]:3
Enter value for disp[1][0]:4
Enter value for disp[1][1]:5
Enter value for disp[1][2]:6
Two Dimensional array elements: 1 2 3
456
Do by yourself.
Matrix Multiplication, Addition,
Subtraction