You are on page 1of 14

Presentation on

Arrays In C

By Mohit Arora

What Are Arrays?


It is a data structure Unlike a variable used to store a single value, e.g int debt = 2; debts debt
1 2

It is used to store series of values of the same type, sequentially The whole array bears a single name, and the individual items, or elements, are accessed by using an integer index
e.g int debts[10];//int is the datatype //debts is name of array //and 20 is the number of elements //contained in an array

How the Compiler recognizes an Array?


Just like variables and functions array have to be declared before their usage. Declaration of array;
/* some array declarations int main(void) { float a[365]; char b[12]; int c[50]; int debts[10]; ... } */

/* /* /* /*

array array array array

of of of of

365 floats */ 12 chars */ 50 ints */ 10 ints */

Above [] brackets identifies a as an array We can access individual elements by writing its index number or subscript in square brackets along with the array name. cont.

How to Access the Elements of Array


In C the index of an Array starts at index 0 and continue until one less then the total i.e. (10-1) in case of debts[10] To access data, just write index of the element, we want, with the name of the array, e.g
Array Index debts[0] debts[1] debts[2] . . . debts[8] debts[9] Array Data 1 2 1

.
. .

X = debts[2];
/*this will transfer the value at index 2 of debts array, i.e. 1 to the variable X*/

5
8

Data Types of Arrays


Arrays can be of all the datatype that C allows, some examples;
int cars[22]; /* an array to hold 22 integers */ char actors[26]; /* an array to hold 26 characters*/ long big[500]; /*an array to hold 500 long integers*/ Which means the spaces occupied in memory would be the size of their respective datatypes for each element of array, e.g. int boo[4]; /* an array to hold 4 integers */ char foo[4]; /* an array to hold 4 char */

Initialization of An Array

A single variable can be initialized as;


int debt = 2;

Array Initialization;
#define NUM = 8; /*constant, a good prog. practice*/

int main(void) { int powers[NUM] = {1,2,4,6,8,16,32,64}; /* ANSI only */


... }

comma-separated list of values enclosed in braces form the elements of an array

Initialization cont.
What if we dont initialize an array;
/* no_data.c -- uninitialized array */ #include <stdio.h> #define SIZE 4 int main(void) { int no_data[SIZE]; /* uninitialized array */ int i; printf("%2s%14s\n", "i", "no_data[i]"); for (i = 0; i < SIZE; i++) printf("%2d%14d\n", i, no_data[i]); return 0; } Like ordinary variables if we dont initialize the array elements they can have any value already present at that memory location. The output is; i no_data[i] 0 16 1 4204937 2 4219854 3 2147348480 Which are values present already in those memory location

Initialization cont.
/* some_data.c -- partially initialized array */ #include <stdio.h> #define SIZE 4 int main(void) { int some_data[SIZE] = {1492, 1066}; int i; printf("%2s%14s\n", "i", "some_data[i]"); for (i = 0; i < SIZE; i++) printf("%2d%14d\n", i, some_data[i]); return 0; } This time the output looks like this: i some_data[i] 0 1492 1 1066 2 0 3 0 Which means partial initialization, also set the other memory locations to 0;

Initialization cont
Another way;
const int days[] = {31,28,31,30,31,30,31,31,30,31}; //compiler sets the number of items to 10 automatically

Designated Initialization;
int arr[6] = {0,0,0,0,0,212}; // traditional syntax int arr[6] = {[5] = 212}; // initialize arr[5] to 212

if we want to initialize just one element of array, former is the traditional way, and later is the new C99 method.

Assigning Values to Arrays

We can use loops for assigning values to array in addition to assignment at initialization

Some wrong methods of assignment;


/* nonvalid array assignment */ #define SIZE 5 int main(void) { int o[SIZE] = {5,3,2,8}; /* ok here */ int y[SIZE]; y = o; /* not allowed */ y[SIZE] = o[SIZE]; /* invalid */ y[SIZE] = {5,3,2,8}; /* doesn't work */ }

Array Bounds

C compiler wont check the boundary of your array, so its the programmer responsibility. Reason! To make C faster. How to avoid!
Use a symbolic constant for length of Array index, Remember that indexing starts at 0 in C.
int n = 5; int m = 8; float a1[5]; float a2[5*2 + 1]; float a4[-4]; float a5[0]; // yes // yes // no, size must be > 0 // no, size must be > 0

float a3[sizeof(int) + 1]; // yes

float a6[2.5];
float a7[(int)2.5]; float a8[n]; float a9[m];

// no, size must be an integer


// yes, typecast float to int constant // not allowed before C99 // not allowed before C99 VLA

Multidimensional Arrays

Declaration;
int box[2][2]; int box[2][2][2]; //uptil 26 in Bloodshed DevC

Initialization;
Int box[2][2] = { {1, 2}, {3, 4} }; //two col and rows Int box[2][2][2] = { { {1,2}, {3, 4} }, { {5,6}, {7, 8} } };

Passing Arrays to Function


#include <stdio.h> int sum(int arr[]); int main(void) { int i ; int arr[4]; long answer; for (i = 0; i < 4; i++) arr[i] = 2*i; answer = sum(arr); for (i = 0; i < 4; i++) printf("index = %d, value = %d\n", i, arr[i]); printf("result = %d\n", answer); } int sum(int arr[]) { int i; int result = 0; for (i = 0; i < 4; i++) result = arr[i] + result; return (result);}