Sunteți pe pagina 1din 4

Chapter 1 Arrays in C

Arrays in C
Definitions:
Array : a finite ordered set of homogenous elements
Finite: specific number of elements.
Ordered: elements in array are arranged .
Homogenous: all elements in array must be the same type.

Lower bound: the smallest element of an array's index.

Upper bound: the highest element of an array's index.

Range: the number of element in the array.


Range = upper bound – lower bound+1

One Dimensional Arrays:


Array declaration in C:
Type arr_name [size];
Example: int a [100];

 Its better to declare a bound as a constant identifier why?


 One dimensional array is the simplest form of array.

Basic operations that access an array:


1. extraction: is a function that accept an array ,a ,and an index ,i, and return an
element of the array.
In C the result of this operation is denoted by the expression a[i].
2. storing: accepts an array ,a ,and an index ,i, and element x.
In C this operation is denoted by the assignment statement a[i]=x.

Implementing One dimensional array:


 In C an array variable is implemented as a pointer variable, so the brackets
automatically imply that variable is a pointer.
 The address of the first location in array is called base address of array, denoted
by base(arr_name)

Example: int b [100];


the type of b is "pointer to integer" or int* .

1 Fatemah Ba_hareth
Chapter 1 Arrays in C

how to calculate the address of an element in array?


Suppose we have array ,b, int b[100];
base(b): The address of the first location in array b.
esize: the size of each element of the array.
the reference of the element b[0]= base(b)
the reference of the element b[1]= base(b)+esize

then the reference of the element b[i]= base(b)+i*esize

implementing an array of varying-sized elements:

if not the all elements have the same size, a different implementation must be used:
 reserve a contiguous set of memory location. Each of which hold an address. The
contents of each memory location is the address of the varying-length array
element in some other portion of memory.
 keep all fixed length portion of the elements in the contiguous array.

 An array of strings is actually an array of arrays, a two-dimensional


rather than a one dimensional array.

Arrays as Parameters:

• Since array variable is a pointer, array parameters are passed by reference.


• Passing an array by reference rather than by value is more efficient in both time
and space.

Character strings in C:

• A string is an array of characters.


• Each string terminated by NULL character.
• The NULL character automatically appended to the end of string when it is stored
within a program.
• The NULL character is denoted by escape sequence \0.
• A string represents an array with lower bound = 0, and upper bound = the number
of character in the string.

Example:
"HELLO THERE" is an array of 12 characters.
(blank and \0 each counts as a character)

2 Fatemah Ba_hareth
Chapter 1 Arrays in C

Character String Operations:

Operation Function prototype Function job


Length int strlen(char s[]) Find the length of the
string
String position int strpos(char s1[],char s2[]) Return the starting of the
first occurrence of the
second parameter string
within the first patameter
string
Concatenation. void strcat(char s1[],char s2[]) Set s1 to the
concatenation of s1 and
s2.
Substring. void substr(char s1[],int i, int j, Set the satring s2 to the j
char s2[]) characters beginning at
s1[i]

Tow-Dimensional Arrays:
declaration in C:
Type arr_name [rows][cols];
Example: int a[3][5];

 The number of rows or columns is called the range of the


dimension.
 A tow-dimensional array is a logical data structure that is useful in
programming and problem solving.

Implementing Tow dimensional array:

• Computer memory is usually linear (one-dimensional array).


• we need to transforming tow dimensional reference to linear representation.

3 Fatemah Ba_hareth
Chapter 1 Arrays in C

• Tow methods to representing a tow dimensional array in memory:


 row-major

 array of pointers

calculate the address of an element in 2 Dim-array (using row-major method):


suppose we have:
int ar[r1][r2];
r1= number of rows ,r2= number of columns.
base(ar): the address of first element in ar.
esize: the size of each element in ar.

Now we want to calculate the address of the element ar[i1][i2]:


The address of the first element in row i1= base(ar)+i1*r2*esize.
Then the address of ar[i1][i2]= base(ar)+(i1*r2+i2)*esize

float arr[10][5];
Calculate the address of:
• arr[4][2]
• arr[5][4]
• arr[7][3]

4 Fatemah Ba_hareth

S-ar putea să vă placă și