SlideShare a Scribd company logo
Array

array is a collection of similar elements.

program to find average marks obtained by a class of 30 students in a test.
main( )
{
int i, avg, sum = 0 ;
int marks[30] ; /* array declaration */
for ( i = 0 ; i <= 29 ; i++ )
{
printf ( "nEnter marks " ) ;
scanf ( "%d", &marks[i] ) ; /* store data in array */
}
for ( i = 0 ; i <= 29 ; i++ )
sum = sum + marks[i] ; /* read data from an array*/
avg = sum / 30 ;

printf ( "nAverage marks = %d", avg ) ;

}

Array Initialisation
int num[6] = { 2, 4, 12, 5, 45, 5 } ;

int n[ ] = { 2, 4, 12, 5, 45, 5 } ;

float press[ ] = { 12.3, 34.2 -23.4, -11.3 } ;

Pointers and Arrays
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 ) ;

}

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

Example:

main( )

{

int num[ ] = { 24, 34, 12, 44, 56, 17 } ;

int i, *j ;
j = &num[0] ; /* assign address(suppose 65512) of zeroth element */
for ( i = 0 ; i <= 5 ; i++ )

{

printf ( "naddress = %u ", j ) ;

printf ( "element = %d", *j ) ;

j++ ; /* increment pointer to point to next location */

}
}

The output of this program would be:
address = 65512 element = 24

address = 65514 element = 34

address = 65516 element = 12

address = 65518 element = 44

address = 65520 element = 56

address = 65522 element = 17



All the statements are same as

num[i]

*( num + i )

*( i + num )

i[num]

Example:

/* Accessing array elements in different ways */
main( )
{
 int num[ ] = { 24, 34, 12, 44, 56, 17 } ;
int i ;
for ( i = 0 ; i <= 5 ; i++ )
{
printf ( "naddress = %u ", &num[i] ) ;
printf ( "element = %d %d ", num[i], *( num + i ) ) ;
printf ( "%d %d", *( i + num ), i[num] ) ; }
}
The output of this program would be:

address = 65512 element = 24 24 24 24

address = 65514 element = 34 34 34 34

address = 65516 element = 12 12 12 12

address = 65518 element = 44 44 44 44

address = 65520 element = 56 56 56 56
address = 65522 element = 17 17 17 17

Two Dimensional Arrays
While initializing a 2-D array it is necessary to mention the second (column) dimension, whereas the
first dimension (row) is optional.e.g.

int stud[4][2] = { { 1234, 56 }, { 1212, 33 }, { 1434, 80 }, { 1312, 78 } } ;

same as

int stud[4][2] = { 1234, 56, 1212, 33, 1434, 80, 1312, 78 } ;

same as

int stud[][2] = { 1234, 56, 1212, 33, 1434, 80, 1312, 78 } ;

Also,

s[2][1]

is same as

* ( s[2] + 1 )

same as

*(*(s+2)+1)

Example:

/* Pointer notation to access 2-D array elements */
main( )
{
int s[4][2] = { { 1234, 56 }, { 1212, 33 }, { 1434, 80 }, { 1312, 78 } } ;
int i, j ;
for ( i = 0 ; i <= 3 ; i++ )
{
printf ( "n" ) ;
for ( j = 0 ; j <= 1 ; j++ )
printf ( "%d ", *( *( s + i ) + j ) ) ;
}
}
And here is the output...
1234 56
1212 33
1434 80

1312 78
Pointer to an Array
/* Usage of pointer to an array */
main( )
{
int s[5][2] = { { 1234, 56 }, { 1212, 33 }, { 1434, 80 }, { 1312, 78 }} ;
int ( *p )[2] ;
int i, j, *pint ;
for ( i = 0 ; i <= 3 ; i++ )
{
p = &s[i] ;
pint = p ;
printf ( "n" ) ;
for ( j = 0 ; j <= 1 ; j++ )
printf ( "%d ", *( pint + j ) ) ;
}
}
And here is the output...
1234 56
1212 33

1434 80

1312 78

Here p is a pointer to an array of two integers. Note that the parentheses in the declaration of p are
necessary. Absence of them would make p an array of 2 integer pointers. In the outer for loop each
time we store the address of a new one-dimensional array. Thus first time through this loop p
would contain the address of the zeroth 1-D array. This address is then assigned to an integer
pointer pint. Lastly, in the inner for loop using the pointer pint we have printed the individual
elements of the 1-D array to which p is pointing.

Passing 2-D Array to a Function
/* Three ways of accessing a 2-D array */
main( )
{
int a[3][4] = {
1, 2, 3, 4,
5, 6, 7, 8,
9, 0, 1, 6
};
clrscr( ) ;
display ( a, 3, 4 ) ;
show ( a, 3, 4 ) ;
print ( a, 3, 4 ) ;
}
display ( int *q, int row, int col )
{
int i, j ;
for ( i = 0 ; i < row ; i++ )
{
for ( j = 0 ; j < col ; j++ )
printf ( "%d ", * ( q + i * col + j ) ) ;

printf ( "n" ) ;
}
printf ("n" ) ;
}
show ( int ( *q )[4], int row, int col )
{
int i, j ;
int *p ;
for ( i = 0 ; i < row ; i++ )
{
p=q+i;
for ( j = 0 ; j < col ; j++ )
printf ( "%d ", * ( p + j ) ) ;
printf ( "n" ) ;
}
printf ( "n" ) ;
}
print ( int q[ ][4], int row, int col )
{
int i, j ;
for ( i = 0 ; i < row ; i++ )
{
for ( j = 0 ; j < col ; j++ )
printf ( "%d ", q[i][j] ) ;
printf ( "n" ) ;
}
printf ( "n" ) ;
}
And here is the output…
1234
5678

9016
1234
5678
9016
1234
5678
9016

In the display( ) function we have collected the base address of the 2-D array being passed to it in
an ordinary int pointer. Then through the two for loops using the expression * ( q + i * col + j ) we
have reached the appropriate element in the array. Suppose i is equal to 2 and j is equal to 3, then
we wish to reach the element a[2][3].
The expression * ( q + i * col + j ) becomes * ( 65502 + 2 * 4 + 3). This turns out to be * (65502 +
11 ). Since 65502 is address of an integer, * ( 65502 + 11 ) turns out to be * (65524). Value at this
address is 6. A more general formula for accessing each array element would be:
* ( base address + row no. * no. of columns + column no.)

In the expression, int ( *q )[4] , q holds the base address of the zeroth 1-D array, i.e. 4001. This address is
then assigned to p, an int pointer, and then using this pointer all elements of the zeroth 1-D array are
accessed. Next time through the loop when i takes a value 1, the expression q + i fetches the address
of the first 1-D array. This is because, q is a pointer to zeroth 1-D array and adding 1 to it would give
us the address of the next 1-D array. This address is once again assigned to p, and using it all
elements of the next 1-D array are accessed.
In the print( ), int q[ ][4] ; is same as int ( *q )[4], where q is pointer to an array of 4 integers. The only
advantage is that we can now use the more familiar expression q[i][j] to access array elements.

Array of Pointers
The addresses present in the array of pointers can be addresses of isolated variables or addresses of
array elements or any other addresses.

main( )
{
int *arr[4] ; /* array of integer pointers */

int i = 31, j = 5, k = 19, l = 71, m ;
arr[0] = &i ;
arr[1] = &j ;
arr[2] = &k ;
arr[3] = &l ;
for ( m = 0 ; m <= 3 ; m++ )
printf ( "%d ", * ( arr[m] ) ) ;
}

More Related Content

PPTX
Algebraic functions powerpoint
PPT
Arrays
KEY
Domain & range intro presentation
PDF
M|18 User Defined Functions
PDF
11 1. multi-dimensional array eng
PDF
Gentle Introduction to Functional Programming
PPT
FP 201 - Unit4 Part 2
PPT
Fp201 unit4
Algebraic functions powerpoint
Arrays
Domain & range intro presentation
M|18 User Defined Functions
11 1. multi-dimensional array eng
Gentle Introduction to Functional Programming
FP 201 - Unit4 Part 2
Fp201 unit4

What's hot (19)

PDF
Java cheatsheet
PPTX
9 the basic language of functions x
PPTX
Arrays
PPSX
Types of functions 05272011
PDF
The java language cheat sheet
PPT
PPT
PDF
N-Queens Combinatorial Problem - Polyglot FP for fun and profit - Haskell and...
PPT
Relations & Functions
KEY
Function Basics Math Wiki
PPTX
Functions
PPT
Multi dimensional arrays
PPTX
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
PPTX
The Algebric Functions
PDF
Cs101 endsem 2014
PPTX
1.4 Functions
DOCX
Function and Its Types.
PDF
Network security CS6
Java cheatsheet
9 the basic language of functions x
Arrays
Types of functions 05272011
The java language cheat sheet
N-Queens Combinatorial Problem - Polyglot FP for fun and profit - Haskell and...
Relations & Functions
Function Basics Math Wiki
Functions
Multi dimensional arrays
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
The Algebric Functions
Cs101 endsem 2014
1.4 Functions
Function and Its Types.
Network security CS6
Ad

Viewers also liked (10)

PPTX
Storyboard writing alif,eva,sri,umi
PDF
Was ist ein Web-Video und wie wird es viral? Thesen, Fakten, Erfahrungen
PPTX
Etm storyboad writing
PDF
Immersive Storytelling: Scrollytelling One-Page Parallax - a short guide how ...
PDF
Storyboarding levels
PPSX
04 storyboard
PPTX
Storyboard
PPT
Presentation Storyboard
PPT
Storyboarding
PPTX
Lesson Plan PowerPoint Presentation
Storyboard writing alif,eva,sri,umi
Was ist ein Web-Video und wie wird es viral? Thesen, Fakten, Erfahrungen
Etm storyboad writing
Immersive Storytelling: Scrollytelling One-Page Parallax - a short guide how ...
Storyboarding levels
04 storyboard
Storyboard
Presentation Storyboard
Storyboarding
Lesson Plan PowerPoint Presentation
Ad

Similar to Array (20)

PPT
Pointers and arrays
PPTX
array ppt of c programing language for exam
PDF
02 arrays
PPTX
Array,MULTI ARRAY, IN C
PPT
array2d.ppt
PPTX
Module_3_Arrays - Updated.pptx............
PPTX
Chapter 13.pptx
PPTX
arrays.pptx
PDF
Array&amp;string
PPTX
Address, Pointers, Arrays, and Structures.pptx
PDF
Homework Assignment – Array Technical DocumentWrite a technical .pdf
PPTX
C (PPS)Programming for problem solving.pptx
PPTX
unit-2-dsa.pptx
PDF
ARRAYS
PPTX
COM1407: Arrays
PPTX
Array
PPT
Basics of Data structure using C describing basics concepts
PPT
c-arrays-pointers.ppt
PPT
c-arrays-pointer basics xxxx yyyy zzzzzzz
Pointers and arrays
array ppt of c programing language for exam
02 arrays
Array,MULTI ARRAY, IN C
array2d.ppt
Module_3_Arrays - Updated.pptx............
Chapter 13.pptx
arrays.pptx
Array&amp;string
Address, Pointers, Arrays, and Structures.pptx
Homework Assignment – Array Technical DocumentWrite a technical .pdf
C (PPS)Programming for problem solving.pptx
unit-2-dsa.pptx
ARRAYS
COM1407: Arrays
Array
Basics of Data structure using C describing basics concepts
c-arrays-pointers.ppt
c-arrays-pointer basics xxxx yyyy zzzzzzz

Array

  • 1. Array array is a collection of similar elements. program to find average marks obtained by a class of 30 students in a test. main( ) { int i, avg, sum = 0 ; int marks[30] ; /* array declaration */ for ( i = 0 ; i <= 29 ; i++ ) { printf ( "nEnter marks " ) ; scanf ( "%d", &marks[i] ) ; /* store data in array */ } for ( i = 0 ; i <= 29 ; i++ ) sum = sum + marks[i] ; /* read data from an array*/ avg = sum / 30 ; printf ( "nAverage marks = %d", avg ) ; } Array Initialisation int num[6] = { 2, 4, 12, 5, 45, 5 } ; int n[ ] = { 2, 4, 12, 5, 45, 5 } ; float press[ ] = { 12.3, 34.2 -23.4, -11.3 } ; Pointers and Arrays 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 ) ;
  • 2. 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 ) ; } 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 Example: main( ) { int num[ ] = { 24, 34, 12, 44, 56, 17 } ; int i, *j ; j = &num[0] ; /* assign address(suppose 65512) of zeroth element */ for ( i = 0 ; i <= 5 ; i++ ) { printf ( "naddress = %u ", j ) ; printf ( "element = %d", *j ) ; j++ ; /* increment pointer to point to next location */ }
  • 3. } The output of this program would be: address = 65512 element = 24 address = 65514 element = 34 address = 65516 element = 12 address = 65518 element = 44 address = 65520 element = 56 address = 65522 element = 17 All the statements are same as num[i] *( num + i ) *( i + num ) i[num] Example: /* Accessing array elements in different ways */ main( ) { int num[ ] = { 24, 34, 12, 44, 56, 17 } ; int i ; for ( i = 0 ; i <= 5 ; i++ ) { printf ( "naddress = %u ", &num[i] ) ; printf ( "element = %d %d ", num[i], *( num + i ) ) ; printf ( "%d %d", *( i + num ), i[num] ) ; } } The output of this program would be: address = 65512 element = 24 24 24 24 address = 65514 element = 34 34 34 34 address = 65516 element = 12 12 12 12 address = 65518 element = 44 44 44 44 address = 65520 element = 56 56 56 56
  • 4. address = 65522 element = 17 17 17 17 Two Dimensional Arrays While initializing a 2-D array it is necessary to mention the second (column) dimension, whereas the first dimension (row) is optional.e.g. int stud[4][2] = { { 1234, 56 }, { 1212, 33 }, { 1434, 80 }, { 1312, 78 } } ; same as int stud[4][2] = { 1234, 56, 1212, 33, 1434, 80, 1312, 78 } ; same as int stud[][2] = { 1234, 56, 1212, 33, 1434, 80, 1312, 78 } ; Also, s[2][1] is same as * ( s[2] + 1 ) same as *(*(s+2)+1) Example: /* Pointer notation to access 2-D array elements */ main( ) { int s[4][2] = { { 1234, 56 }, { 1212, 33 }, { 1434, 80 }, { 1312, 78 } } ; int i, j ; for ( i = 0 ; i <= 3 ; i++ ) { printf ( "n" ) ; for ( j = 0 ; j <= 1 ; j++ ) printf ( "%d ", *( *( s + i ) + j ) ) ; } } And here is the output... 1234 56 1212 33 1434 80 1312 78
  • 5. Pointer to an Array /* Usage of pointer to an array */ main( ) { int s[5][2] = { { 1234, 56 }, { 1212, 33 }, { 1434, 80 }, { 1312, 78 }} ; int ( *p )[2] ; int i, j, *pint ; for ( i = 0 ; i <= 3 ; i++ ) { p = &s[i] ; pint = p ; printf ( "n" ) ; for ( j = 0 ; j <= 1 ; j++ ) printf ( "%d ", *( pint + j ) ) ; } } And here is the output... 1234 56 1212 33 1434 80 1312 78 Here p is a pointer to an array of two integers. Note that the parentheses in the declaration of p are necessary. Absence of them would make p an array of 2 integer pointers. In the outer for loop each time we store the address of a new one-dimensional array. Thus first time through this loop p would contain the address of the zeroth 1-D array. This address is then assigned to an integer pointer pint. Lastly, in the inner for loop using the pointer pint we have printed the individual elements of the 1-D array to which p is pointing. Passing 2-D Array to a Function /* Three ways of accessing a 2-D array */ main( ) { int a[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 6 }; clrscr( ) ; display ( a, 3, 4 ) ; show ( a, 3, 4 ) ; print ( a, 3, 4 ) ; } display ( int *q, int row, int col ) {
  • 6. int i, j ; for ( i = 0 ; i < row ; i++ ) { for ( j = 0 ; j < col ; j++ ) printf ( "%d ", * ( q + i * col + j ) ) ; printf ( "n" ) ; } printf ("n" ) ; } show ( int ( *q )[4], int row, int col ) { int i, j ; int *p ; for ( i = 0 ; i < row ; i++ ) { p=q+i; for ( j = 0 ; j < col ; j++ ) printf ( "%d ", * ( p + j ) ) ; printf ( "n" ) ; } printf ( "n" ) ; } print ( int q[ ][4], int row, int col ) { int i, j ; for ( i = 0 ; i < row ; i++ ) { for ( j = 0 ; j < col ; j++ ) printf ( "%d ", q[i][j] ) ; printf ( "n" ) ; } printf ( "n" ) ; } And here is the output… 1234 5678 9016 1234 5678 9016 1234 5678 9016 In the display( ) function we have collected the base address of the 2-D array being passed to it in an ordinary int pointer. Then through the two for loops using the expression * ( q + i * col + j ) we have reached the appropriate element in the array. Suppose i is equal to 2 and j is equal to 3, then we wish to reach the element a[2][3].
  • 7. The expression * ( q + i * col + j ) becomes * ( 65502 + 2 * 4 + 3). This turns out to be * (65502 + 11 ). Since 65502 is address of an integer, * ( 65502 + 11 ) turns out to be * (65524). Value at this address is 6. A more general formula for accessing each array element would be: * ( base address + row no. * no. of columns + column no.) In the expression, int ( *q )[4] , q holds the base address of the zeroth 1-D array, i.e. 4001. This address is then assigned to p, an int pointer, and then using this pointer all elements of the zeroth 1-D array are accessed. Next time through the loop when i takes a value 1, the expression q + i fetches the address of the first 1-D array. This is because, q is a pointer to zeroth 1-D array and adding 1 to it would give us the address of the next 1-D array. This address is once again assigned to p, and using it all elements of the next 1-D array are accessed. In the print( ), int q[ ][4] ; is same as int ( *q )[4], where q is pointer to an array of 4 integers. The only advantage is that we can now use the more familiar expression q[i][j] to access array elements. Array of Pointers The addresses present in the array of pointers can be addresses of isolated variables or addresses of array elements or any other addresses. main( ) { int *arr[4] ; /* array of integer pointers */ int i = 31, j = 5, k = 19, l = 71, m ; arr[0] = &i ; arr[1] = &j ; arr[2] = &k ; arr[3] = &l ; for ( m = 0 ; m <= 3 ; m++ ) printf ( "%d ", * ( arr[m] ) ) ; }