0% found this document useful (0 votes)
8 views

Array

The document discusses arrays and strings in C programming. It defines arrays as collections of similar elements of the same data type stored in contiguous memory locations. The key types of arrays are 1D and 2D arrays. It also covers initializing and accessing array elements, the relationship between arrays and pointers, and how to calculate the memory location of an array element. Finally, it defines strings as character arrays terminated with a null character and discusses string declaration and representation in memory.

Uploaded by

Raja Ans
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Array

The document discusses arrays and strings in C programming. It defines arrays as collections of similar elements of the same data type stored in contiguous memory locations. The key types of arrays are 1D and 2D arrays. It also covers initializing and accessing array elements, the relationship between arrays and pointers, and how to calculate the memory location of an array element. Finally, it defines strings as character arrays terminated with a null character and discusses string declaration and representation in memory.

Uploaded by

Raja Ans
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

G.

N College , BCA [NOTES BY KALYAN SIR]

PROGRAMMING AND DATA STRUCTURE

1 ARRAY

1. ARRAYS

∙ Array is a collection of similar elements having same data type, accessed using a common
name.
∙ An array is a collection of items stored at contiguous memory locations. The idea is to store
multiple items of the same type together. This makes it easier to calculate the position of
each element by simply adding an offset to a base value, i.e., the memory location of the
first element of the array.
∙ Array elements occupy contiguous memory locations.
∙ If first element is “i” and last element is “j” then:
Number of elements before j = j - i
Number of elements including j = j – i + 1
Declaration of an Array: type
variable[num_elements];
Example: int A[100];
∙ It creates an array A with 100 integer elements.
∙ The size of an array A can’t be changed.
∙ We cannot declare an array without assigning size. If we declare an array without size,
it will throw a compile time error.
∙ The number between the brackets must be a constant.

2. TYPES OF ARRAYS

One dimensional (1-D) arrays or Linear arrays:


In it each element is represented by a single subscript. The elements are stored in
consecutive memory locations. E.g. A [1], A [2], ….., A [N].
The [] is used for dimensional or the subscript of the array that is generally used for
declaring the elements of the array. For Accessing the Element from the array we can
use the Subscript of the Array like this
a[3]=4

2
G. N College , BCA [NOTES BY KALYAN SIR]

This will set the value of 4th element of array.

Two dimensional (2-D) arrays or Matrix arrays:


In it each element is represented by two subscripts. Thus, a two dimensional m x n array
A has m rows and n columns and contains m*n elements. It is also called matrix array
because in it the elements form a matrix. E.g. A [3] [4] has 3 rows and 4 columns and
3*4 = 12 elements.
Column1 Column2 Column3 Column4

Row1 a[0] [0] a[0] [1] a[0] [2] a[0] [3]

Row2 a[1] [0] a[1] [1] a[1] [2] a[1] [3]

Row3 a[2] [0] a[2] [1] a[2] [1] a[2] [3]

3. INITIALIZATION OF AN ARRAY

∙ int A[5]= {1,2,3,4,5}; /*Array can be initialized during declaration*/


∙ int A[5]={1,2,3}; /* Remaining elements are automatically initialized to zero*/
∙ int A[5]={1,[1]=2, 3,4,[4]=0};/* Array element can be initialized by specifying its index
location*/

Note:

In programming we use * or dereference operator to find the value of the location.

4. POINTERS & ARRAYS

int a[10] : It will be read as an array of 10 elements with integer data type.
∙ Every array contains a base address which is the address of the first element of the array.
∙ An array variable is just a pointer to the first element in the array.
∙ You can access array elements using array notation or pointers.
∙ print a will print the base address of the array.
∙ a[0] is the same as *a
∙ a[1] is the same as *(a + 1)
∙ a[2] is the same as *(a + 2)
∙ a = a+0 = &a[0]
∙ a+1 = &a[1]

3
G. N College , BCA [NOTES BY KALYAN SIR]

∙ a+i = &a[i]
∙ &(*(a+i)) = &a[i] = a+i
∙ *(&a[i]) = *(a+i) = a[i]
∙ Address of an element i of array a = a + i * sizeof(element)
Let b be the 2- Dimensional Array b[i][j]
∙ *(*(b + i) + j) is equivalent to b[i][j], here first * is used to select the rows and the second
* selects the element.
∙ &b +1 means the whole 2-D array is skipped.
∙ &b gives the base address of the array.
∙ *b + 1 skips one element.
∙ *(b + i) + j is equivalent to &b[i][j]
∙ *(b[i] + j) is equivalent to b[i][j]
∙ b[i] + j is equivalent to &b[i][j]
∙ (*(b+i))[j] is equivalent to b[i][j]

5. CALCULATING LOCATION OF AN ELEMENT IN AN ARRAY

1-D Arrays
Consider a single dimensional array as A[lb ----- ub]
The base address of array = BA
Size of each element in array = c
Total elements in array is given by (ub-lb+1)

Then address of any random element A[i] is given by :

Location[A[i]] = BA + (i-lb)*c , where c is size of the element

Example 1: Let A [78...... 380] , Base address = 1000, size of element = 10


Then LOC (A [250]) = 1000 + (250 – 78) * 10
= 1000 + 2150
= 3150
Example 2: Let A[ -50 ..... 100] , Base Address = 1000, size of element = 10
Then LOC( A [60]) = 1000 + (60 – (-50)) * 10
= 1000 + 1100
= 2100
2-D Arrays
2-D arrays can be stored in the system in two ways: Row Major order and Column Major
order.
Consider a 2-D array as A[lb1-----ub1] [lb2 ---------ub2]

4
G. N College , BCA [NOTES BY KALYAN SIR]

The base address of array = BA


Size of each element in array = c
Number of rows = ub1-lb1+1
Number of columns = ub2-lb2+1
In Row Major order, then address of any random element A[i][j] is given by :

Location[A[i][j]] = BA + [(i-lb1)*(ub2-lb2+1)+(j-lb2)]*c, where c is the size of an element.

Where, (i-lb1) gives the number of rows before i,


(ub2-lb2+1) gives the number of column and
(j-lb2) tells the number of columns before j
In Column Major order, then address of any random element A[i][j] is given by :

Location[A[i][j]] = BA + [(j-lb2)*(ub1-lb1+1)+(i-lb1)]*c, where c is the size of an element.

Where, (j-lb2) gives the number of column before j ,


(ub1-lb1+1) gives the number of rows and
(i-lb1) tells the number of rows before i
Example 3: Let A [30…….80 , 3……150] , Base Address = 1000 , size of element = 10
Find LOC( A[73][120]).
Sol: Number of columns = 150 – 3 + 1 = 152
Number of rows = 80 – 30 + 1 = 51
Using Row major:
LOC( A[73][120]) = 1000 + [ (73 – 30)* 152 + (120 – 3)] * 10
= 1000 + 54920
= 55920
Using Column Major:
LOC( A[73][120]) = 1000 + [ (120 – 3)* 51 + (73 – 30)]*10
= 1000 + 59713
= 60713
Example 4: Let A [-25…….75, -35…….125] , Base address = 1000, Size of element = 10
Find LOC( A[50][40])
Sol: Number of columns = 75 – (-25) + 1 = 101
Number of rows = 125 – (-35) + 1 = 161
Using Row major:
LOC( A[50][40]) = 1000 + [ (50-(-25))*101 + (40 – (-35)) *10
= 1000 + (75 *101 + 75) * 10
= 77500
Using Column Major:
LOC( A[50][40]) = 1000 + [ (40 – (-35)) * 161 + ((50-(-25))] *10
= 1000 + 121500
= 122500

5
G. N College , BCA [NOTES BY KALYAN SIR]

6. STRINGS

∙ Strings are defined as an array of characters. The difference between a character array and
a string is the string is terminated with a special character ‘\0’.
∙ A string is a sequence of characters terminated with a null character \0
∙ Declaring a string is as simple as declaring a 1- Dimensional array.
∙ Below is the basic syntax for declaring a string in C programming language.
char str_name[size];
String Declaration
1. char str [] = {‘A’, ‘B’, ‘C’, ‘D’, ‘\0’};
2. char st [] = “ABCD”;
𝖴
‘\0’ would automatically
Inserted at the end in this type of declaration
Example 5: The string "hello world" contains 12 characters including '\0'
∙ Before the string class, the abstract idea of a string was implemented with just an array of
characters. For example, here is a string:
char label[] = "Single";
The array in the memory will be as follows:

S i n g l e \0
where the beginning of the array is at some location in computer memory.
∙ A character array can have more characters than the abstract string held in it, as below:
char label [10] = "Single";
The array will look like:

S i n g l e \0

(where 3 array elements are currently unused).

****

You might also like