Array
Array
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
2
G. N College , BCA [NOTES BY KALYAN SIR]
3. INITIALIZATION OF AN ARRAY
Note:
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]
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)
4
G. N College , BCA [NOTES BY KALYAN SIR]
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
****