Chapter 6 - Arrays: Outline
Chapter 6 - Arrays: Outline
Outline
6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 Introduction Arrays Declaring Arrays Examples Using Arrays Passing Arrays to Functions Sorting Arrays Case Study: Computing Mean, Median and Mode Using Arrays Searching Arrays Multiple-Subscripted Arrays
6.1 Introduction
Arrays
Structures of related data items Static entity same size throughout program Dynamic data structures discussed in Chapter 12
6.2 Arrays
Array To refer to an element, specify
Array name Position number
Name of array (Note that all elements of this array have the same name, c)
c[0] -45 6 0 72
Group of consecutive memory locations c[1] c[2] Same name and type
c[3]
c[4]
c[5] c[6] c[7] c[8] c[9] c[10] c[11]
1543
-89 0 62 -3 1 6453 78
Format:
arrayname[ position number ]
First element at position 0 n element array named c: c[ 0 ], c[ 1 ]...c[ n 1 ]
6.2 Arrays
Array elements are like normal variables
c[ 0 ] = 3; printf( "%d", c[ 0 ] );
Perform operations in subscript. If x equals 3
c[ 5 - 2 ] == c[ 3 ] == c[ x ]
The dimension of an array is determined by the number of indices needed to uniquely identify each element.
An index is a positive integer enclosed in [ ] placed immediately after the array name.
Defining an Array
An array has some particular characteristics and has to be defined with them. These characteristics include -
Defining an Array - I
An array is defined in the same way as a variable is defined except that the array name is followed by one or more expressions, enclosed within square brackets [ ], specifying the array dimension.
Each element of an array can be used anywhere that a variable is allowed or required.
Arrays can have their data types like int, char, float
Array Handling In C
Array Handling In C
An array is treated differently from a variable in C. Two arrays, even if they are of the same type and size cannot be tested for equality.
Values cannot be assigned to an array on the whole, instead values are assigned to the elements of the array.
Array Initialization
Each element of an Automatic array needs to be initialized separately. In the following example the array elements have been assigned valued using the for loop.
In case of extern and static arrays, the elements are automatically initialized to zero.
The input for the above is of 4 characters and the 5th character is the null character.
String Functions
C supports a wide range of string functions, which are found in the standard header file <string.h> Some of them are listed below -
A two - dimensional array looks like a school time-table consisting of rows and columns.
int n[ 5 ] = { 0 }
All elements 0
If too many a syntax error is produced syntax error C arrays have no bounds checking
Can access individual characters string1[ 3 ] is character s Array name is address of array, so & not needed for
scanf
Reads characters until whitespace encountered Can write beyond end of array, be careful
22
1 2 3 4 5 6
1. Initialize strings
int main() {
7
8 9 10 11 12 13 14 15 16 17 18 19 20 21 }
2. Print strings
printf( "\n" ); return 0; string: Hello there is: Hello is: string literal with spaces between characters is: o
25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/* Fig. 6.13: fig06_13.c Passing arrays and individual array elements to functions */ #include <stdio.h> #define SIZE 5
1. Function definitions
void modifyArray( int [], int ); void modifyElement( int ); /* appears strange */
int main() { int a[ SIZE ] = { 0, 1, 2, 3, 4 }, i; printf( "Effects of passing entire array call " "by reference:\n\nThe values of the " "original array are:\n" ); for ( i = 0; i <= SIZE - 1; i++ ) printf( "%3d", a[ i ] );
2. Pass array to a function 2.1 Pass array element to a function Entire arrays passed3. Print call-byreference, and can be modified
printf( "\n" ); modifyArray( a, SIZE ); /* passed call by reference */ printf( "The values of the modified array are:\n" ); for ( i = 0; i <= SIZE - 1; i++ ) printf( "%3d", a[ i ] );
printf( "\n\n\nEffects of passing array element call " "by value:\n\nThe value of a[3] is %d\n", a[ 3 ] ); modifyElement( a[ 3 ] ); printf( "The value of a[ 3 ] is %d\n", a[ 3 ] ); return 0; }
33 34 void modifyArray( int b[], int size ) 35 { 36 37 38 39 40 } 41 42 void modifyElement( int e ) 43 { 44 45 } Effects of passing entire array call by reference: The values of 0 1 2 3 The values of 0 2 4 6 the original array are: 4 the modified array are: 8 printf( "Value in modifyElement is %d\n", e *= 2 ); for ( j = 0; j <= size - 1; j++ ) b[ j ] *= 2; int j;
Program Output
Example:
29
elements
30 element array takes at most 5 steps
25 > 30 so at most 5 steps
30
31
Initialization
Initializers grouped by row in braces
1 3
int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };
0 4
Referencing elements
Specify row, then column printf( "%d", b[ 0 ][ 1 ] );
32
Session 7
What is a Pointer?
A pointer is a variable, which contains the address of a memory location of another variable.
If one variable contains the address of another variable, the first variable is said to point to the second variable.
Pointers can point to variables of other fundamental data types like int, char, or double or data aggregates like arrays or structures.
To return more than one value from a function. To pass arrays and strings more conveniently from one function to another.
To manipulate arrays easily by moving pointers to them instead of moving the arrays itself.
Pointer Variables
If a variable is to be used as a pointer, it must be declared. A pointer declaration consists of a base type, a *, and a variable name.
For Example:
Pointer Operators
There are 2 special operators which are used with pointers :
The second operator unary operator and it of &. It the memory address The & operator is a * is the complement returns is a unary operator andthe operand.value contained in the memory location pointed to by of returns the the pointer variables value.
It is also possible to assign values to pointers through another pointer variable pointing to a data item of the same data type.
The above declaration will assign 10 to the variable var if ptr_var points to var.
Pointer Arithmetic
Addition and subtraction are the only operations that can be performed on pointers.
Take a look at the following example :
Let var be an integer type variable having the value 500 and stored at the address 1000.
Then ptr_var has the value 1000 stored in it. Since integers are 2 bytes long, after the expression ptr_var++; ptr_var will have the value as 1002 and not 1001.
Each time a pointer is incremented, it points to the memory location of the next element of its base type.
All other pointers will increase or decrease depending on the length of the data type they are pointing to.
Pointer Comparisons
Two pointers can be compared in a relational expression provided both the pointers are pointing to variables of the same type. Consider that ptr_a and ptr_b are 2 pointer variables, which point to data elements a and b. In this case the following comparisons are possible:
The address of the data item is passed and thus the function can freely access the contents of that address from within the function
In this way, function arguments permit data-items to be altered in the calling routine and the function.
When the arguments are pointers or arrays, a call by reference is made to the function as opposed to a call by value for the variable arguments.
Function Definition
indicates that the arguments ptr_str points to type char and ptr_int points to type int.
where pstr is declared as a pointer and the address of the variable var is passed.
Function Definition - I
Assigning a value through,
in the function can now assign values to the variable var in the calling routine, enabling a two way transfer to and from the function.
The following program shows the relationship between array elements and their addresses.
instead of
Allocating Memory
The following example uses a two-dimensional array to record marks of 5 students for 10 subjects.