Seminar 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

RP SARATHY INSTITUTE OF TECHNOLOGY-SALEM

SEMINAR
CS3353-C PROGRAMMING AND DATA STRUCTURES

C Arrays
An array in C is a fixed-size collection of similar data items stored in
contiguous memory locations.
It can be used to store the collection of primitive data types such as int,
char, float, etc., and also derived and user-defined data types such as pointers,
structures, etc.

In C, we have to declare the array like any other variable before using it.
We can declare an array by specifying its name, the type of its elements, and
the size of its dimensions.
When we declare an array in C, the compiler allocates the memory
block of the specified size to the array name.
Syntax of Array Declaration
data_type array_name [size];
or
data_type array_name [size1] [size2]...[sizeN];

Where N is the number of dimensions.

The C arrays are static in nature, i.e., they are allocated memory at the
compile time.
Example of Array Declaration
// C Program to illustrate the array declaration
#include <stdio.h>
int main()
{
// declaring array of integers
int arr_int[5];
// declaring array of characters
char arr_char[5];
return 0;
}
C Array Initialization
Initialization in C is the process to assign some initial value to the
variable.
When the array is declared or allocated memory, the elements of the
array contain some garbage value. So, we need to initialize the array to some
meaningful value.
There are multiple ways in which we can initialize an array in C .
1.Array Initialization with Declaration
In this method, we initialize the array along with its declaration. We use
an initializer list to initialize multiple elements of the array.
An initializer list is the list of values enclosed within
braces { } separated b a comma.
Syntax
data_type array_name [size] = {value1, value2, ... valueN};

2. Array Initialization with Declaration without Size


If we initialize an array using an initializer list, we can skip declaring
the size of the array as the compiler can automatically deduce the size of the
array in these cases.
The size of the array in these cases is equal to the number of elements
present in the initializer list as the compiler can automatically deduce the size
of the array.
data_type array_name[] = {1,2,3,4,5};

The size of the above arrays is 5 which is automatically deduced by the


compiler.
3. Array Initialization after Declaration (Using Loops)
We initialize the array after the declaration by assigning the initial value
to each element individually.
We can use for loop, while loop, or do-while loop to assign the value to
each element of the array.

for (int i = 0; i < N; i++) {


array_name[i] = valuei;
}

Example of Array Initialization in C


// C Program to demonstrate array initialization

#include <stdio.h>

int main()

// array initialization using initialier list

int arr[5] = { 10, 20, 30, 40, 50 };

// array initialization using initializer list without

// specifying size

int arr1[] = { 1, 2, 3, 4, 5 };

// array initialization using for loop

float arr2[5];

for (int i = 0; i < 5; i++)

arr2[i] = (float)i * 2.1;

return 0;

}
Access Array Elements
We can access any element of an array in C using the array subscript
operator [ ] and the index value i of the element.
array_name [index];
One thing to note is that the indexing in the array always starts with 0,
i.e., the first element is at index 0 and the last element is at N – 1 where N is
the number of elements in the array.

Types of Array in C
There are two types of arrays based on the number of dimensions it has.
They are as follows:
1. One Dimensional Arrays (1D Array)
2. Multidimensional Arrays

1. One Dimensional Array in C


The One-dimensional arrays, also known as 1-D arrays in C are those
arrays that have only one dimension.
Syntax of 1D Array in C
array_name [size];

Array of Characters (Strings)


In C, we store the words, i.e., a sequence of characters in the form of an
array of characters terminated by a NULL character. These are called strings
in C language.
Syntax
Char arr[];

2. Multidimensional Array in C
Multi-dimensional Arrays in C are those arrays that have more than one
dimension. Some of the popular multidimensional arrays are 2D arrays and 3D
arrays. We can declare arrays with more dimensions than 3d arrays but they
are avoided as they get very complex and occupy a large amount of space.
A. 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 2D Array in C
array_name[size1] [size2];
Here,
 size1: Size of the first dimension.
 size2: Size of the second dimension.

B. Three-Dimensional Array in C
 Another popular form of a multi-dimensional array is Three
Dimensional Array or 3D Array. A 3D array has exactly three
dimensions. It can be visualized as a collection of 2D arrays stacked on
top of each other to create the third dimension.
 Syntax of 3D Array in C
array_name [size1] [size2] [size3];

You might also like