Unit 3. Derived data types in C
- Arrays: One Dimensional arrays - Declaration, Initialization and Memory representation Two Dimensional arrays -Declaration, Initialization and Memory representation.
- Strings: Declaring & Initializing string variables; String handling functions, Character handling functions.
Arrays
An array is a collection and group of elements of the same data type that share a common name and are stored in contiguous memory locations. An array can store many elements depending on the given size.
Example:
int a[5] = {15, 20, 25, 30, 35};Array indexing:
a[0] = 15a[1] = 20a[2] = 25a[3] = 30a[4] = 35
Types of Arrays
- One Dimensional Arrays - Data is organized linearly in only one direction
- Two Dimensional Arrays - Can have only two dimensions, data is shown in the form of matrices
- Multidimensional Arrays - Can have 3 or more dimensions. In 3D arrays, data can be organized in cubes
One Dimensional Arrays
C provides two types of arrays based on their size:
1. Fixed Length Array In fixed length arrays, the size of the array is known when the program is written. An array should be declared before using it in our program.
Syntax:
data_type array_name[size_of_array];int a[10];2. Variable Length Array
In variable length arrays, the size of the array is not known when the program is written.
int n;
scanf("%d", &n); // User enters 30
int a[n]; // a[30]Initialization of Arrays
After declaring an array, it should be initialized with some values. Initialization can be done in 2 ways.
1. Compile Time Initialization
In this, we can assign values to the array directly in the program.
Syntax:
data_type array_name[size] = {list of values};int a[5] = {1, 2, 3, 4, 5};Initialize without size:
int a[] = {1, 2, 3, 4, 5};Partial Initialization:
int a[5] = {1, 2}; // Remaining elements are initialized to 0Program Example:
#include <stdio.h>
int main() {
int a[5] = {10, 20, 2, 4, 7};
int i;
for (i = 0; i < 5; i++) {
printf("%d\t", a[i]);
}
return 0;
}Output:
10 20 2 4 7- Run Time Initialization In this, we have to assign values during runtime.
#include <stdio.h>
int main() {
int i, a[5];
printf("Enter Array Values: ");
for (i = 0; i < 5; i++) {
scanf("%d", &a[i]);
}
printf("Array elements are: ");
for (i = 0; i < 5; i++) {
printf("%d\t", a[i]);
}
return 0;
}Output:
Enter Array Values: 1 2 3 4 5
Array elements are: 1 2 3 4 5