Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 19

ARRAYS

Sequence of memory elements of same data type. Sequence of 10 integers stored one after another in memory represents an array. int a[10];

ARRAYS
Fixed number of data items of same type. Data items-Elements Type int ,float and long etc.. Array of datatype character - strings

ARRAY Declaration
Data type int a[10] Name of array Size of array

a array of 10 integers Square braces represents number of elements Array index starts from a[0] as shown below:
Note: In

C, array indices always begin with zero.

Accessing ARRAY Elements


28 15 86 54 78 21 34 7 91 67

Array index points to particular elements in an array. Example:


a[3]=54 a[7]=7 a[5]=21 a[10]=?

Usage of for loop


Commonly used to access array elements. for(i=0;i<9;i++) { printf(%d \n, a[i]); } Prints the array elements If you try to access the undefined element, result will be unpredictable.(compilation will be successful)

Initialization of ARRAY
Can be initialized once they are declared. int a[10]={28,15,86,54,78,21,34,7,91,67}; Values should be given in curly brackets. Less than 10 elements-0 will be filled. Greater than 10 elements-excessive element declared warning will be reported. List of values-separated by commas.

Name of the array


Take the case where a is an array of 10 integers. a-Name of the array If a is used without any subscript in the printf, what will be the output? What will be the output of the following statement? printf(%d, sizeof(a)); Note : a =&a[0]

ARRAYS
To handle matrix and tables, we need array with multiple subscripts. Two-dimensional array-two subscripts Row and column-starts with zero 2-D array can also be called as matrix.

2-D ARRAY Declaration


Data type

float marks[4][3]
Name of the array 0 1 40.5 55.5 70.0 85.0 2 45.5 60.5 75.0 90.0

Size of an array 4 rows 3 columns

0 1 2 3

35.5 50.5 65.0 80.0

Elements are accessed as follows: Marks[0][0]=35.5 Marks[1][1]=55.5 Marks[2][1]=70.0 Marks[3][1]=85.0

Storage of 2-D ARRAY


200 0 200 4 200 8 201 2 201 6 202 0 202 4 202 8 203 2 203 6 204 0 204 4

Marks[0][0] Marks[0][1]

Marks[1][0]

Marks[2][0]

Marks[3][0]

Initialization: Float marks[4][3]={

{35.5,40.5,45.5}, {50.5,55.5,60.5}, {65.0,70.0,75.0}, {80.0,85.0,90.0} };

Passing ARRAY to a Function


/*Pass by Value*/ main( ) { int i ; int marks[ ] = { 55, 65, 75, 56, 78, 78, 90 } ; for ( i = 0 ; i <= 6 ; i++ ) display ( marks[i] ) ; } display ( int m ) { printf ( "%d ", m ) ; }

Passing ARRAY to a Function


/* Demonstration of call by reference */ main( ) { int i ; int marks[ ] = { 55, 65, 75, 56, 78, 78, 90 } ; for ( i = 0 ; i <= 6 ; i++ ) disp ( &marks[i] ) ; } disp ( int *n ) { printf ( "%d ", *n ) ; }

Write a program to add two matrices using function Eg: function addmat()

POINTERS AND ARRAYS


Facts to be considered:
Array elements are always stored in contiguous memory locations. Pointer when incremented always points to immediately next location of its type.

POINTER TO ARRAY
main() { int num[ ]={24,34,12,44,56,17}; int i=0,*j; j=&num[0]; while(i<=5) { printf(Address=%u \n,&num[i]); printf(Element=%d \n,*j); i++; j++; } }

POINTER TO ARRAY
Accessing array elements using pointer is faster than accessing them by subscripts. Should be accessed using pointers, if the elements are to be accessed in a fixed order. If there is no fixed logic, then subscript can be used but pointer will be faster always. Pointer notations: num[i] = *(num+i) = *(i+num) = i[num]

I request Electronics and communication ENGINEERING students to visit my blog for more abhishek1ek.blogspot.com awhengineering.blogspot.com

You might also like