UNIT-3
UNIT-3
Contents
Arrays
Strings
Arrays
An array is a homogeneous collection of
elements of the same types of data stored in
contiguous memory locations.
Here comes the use of arrays in C, where we can store multiple data
type values in a contiguous memory block. Hence, we can use an
array in C when we are working with a large number of similar items.
Array in C Initialization and
Declaration
An array may be initialized at the time of declaration.
Giving initial values to an array.
This is done because when declaring arrays in C, they contain garbage
values inside, which can alter our desired output.
In this declaration method, we will use the loops to declare our array. We will
use the loop to assign values to each element and insert them in our array.
Examples : # include <stdio.h>
#include <stdio.h>
int main() {
Syntax of 1D Array in C:
array_name [size];
Output
The array elements are : 10 20 30 40 50
Array of Characters (Strings)
Output
Geeks
Two-Dimensional Array in C : A Two-Dimensional array or 2D array in C is an
array that has exactly two dimensions. They can be visualized in the form of rows
and columns organized in a two-dimensional plane.
Syntax of 2-D Array : Examples :
array_name[size1] [size2]; int xInteger[3][4];
Here, size1: Size of the first dimension. float matrixNum[20][25];
Output
2D
Array: Output :
10 20 30 a[0][0] = 0
40 50 60 a[0][1] = 0
a[1][0] = 1
a[1][1] = 2
a[2][0] = 2
a[2][1] = 4
a[3][0] = 3
a[3][1] = 6
a[4][0] = 4
a[4][1] = 8
A 2D array with m rows and n columns can be created as]:
Data_type arr_name[m][n];
where,
•type: Type of data to be stored in each element.
Time Complexity: O(n * m), where n and m are numbers of rows and columns respectively.
C Array Traversal
Traversal is the process in which we visit every element of the data structure. For
C array traversal, we use loops to iterate through each element of the array.
array_name[i];
}
Properties of
Arrays
1. Fixed Size
The array in C is a fixed-size collection of elements. The size of the array must be known at the compile time and it cannot be
changed once it is declared.
2. Homogeneous Collection
We can only store one type of element in an array. There is no restriction on the number of elements but the type of all of these
elements must be the same.
3. Indexing in Array
The array index always starts with 0 in C language. It means that the index of the first element of the array will be 0 and the last
element will be N – 1.
4. Dimensions of an Array
A dimension of an array is the number of indexes required to refer to an element in the array. It is the number of directions in
which you can grow the array size.
5. Contiguous Storage
All the elements in the array are stored continuously one after another in the memory. It is one of the defining
properties of the array in C which is also the reason why random access is possible in the array.
6. Random Access
The array in C provides random access to its element i.e we can get to a random element at any index of the array in
constant time complexity just by using its index number.
7. No Index Out of Bounds Checking
There is no index out-of-bounds checking in C/C++, for example, the following program compiles fine but may
produce unexpected output when run.
Advantages of Array in C
The following are the main advantages of an array:
Random and fast access of elements using the array index.
Use of fewer lines of code as it creates a single array of multiple elements.
Traversal through the array becomes easy using a single loop.
Sorting becomes easy as it can be accomplished by writing fewer lines of code.
Disadvantages of Array in C
Allows a fixed number of elements to be entered which is decided at the time of declaration.
Unlike a linked list, an array in C is not dynamic.
Insertion and deletion of elements can be costly since the elements are needed to be
rearranged after insertion and deletion.
Why the index of array always start with 0?
Array indexes start at zero because it makes code easier to read and understand, and it's
more efficient.
reasons
•Readability: It's easier to follow when the index of the first element is 0, not 1.
•Efficiency: When the index starts at 0, the computer doesn't need to add 1 to calculate the
memory address of an element.
•Loop conditions: Starting at 0 simplifies loop conditions and avoids errors.
•Arithmetic: It's easier to perform arithmetic operations on array indices.
•Binary numbering: Starting at 0 aligns well with binary numbering, making it easier to
String in C
A string in C is a one-dimensional array of char type, with the last
character in the array being a "null character" represented by '\0'.
Thus, a string in C can be defined as a null-terminated sequence of
char type values.
string_name is any name given to the string variable and size is used to
define the length of the string
C String Initialization
We can initialize a string in 4 different ways which are as follows:
1. Assigning a string literal without size
String literal can be assigned without size. Here, the name of the string str acts as a
pointer because it is an array.
Syntax: char str[] = “helloworld” ;
2. Assigning a string literal with predefined size
If we want to store a string of size n then we should always declare a string with a
size equal to or greater than n+1. one extra space for null character.
Syntax: char str[50] = “helloworld” ;
3. . Assigning Character by Character with Size
We can also assign a string character by character. But we should remember to set
the end character as ‘\0’ which is a null character.
Syntax: char str[10] = {‘h’ , ‘e’ , ‘l’ , ‘l’ , ‘o’ , ‘w’ , ‘o’ , ‘r’ , ‘l’ , ‘d’ , ‘\0’} ;
4. Assigning Character by Character without Size
We can assign character by character without size with the NULL character at the
end.
Syntax: char str[] = {‘h’ , ‘e’ , ‘l’ , ‘l’ , ‘o’ , ‘w’ , ‘o’ , ‘r’ , ‘l’ , ‘d’ , ‘\0’} ;