0% found this document useful (0 votes)
14 views19 pages

UNIT-3

The document provides an overview of arrays and strings in C programming, explaining their definitions, types, and usage. It covers one-dimensional and two-dimensional arrays, their initialization, properties, advantages, and disadvantages, as well as the concept of strings as null-terminated character arrays. Key points include the fixed size of arrays, random access capabilities, and the importance of the null character in strings.

Uploaded by

itzzmee664
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views19 pages

UNIT-3

The document provides an overview of arrays and strings in C programming, explaining their definitions, types, and usage. It covers one-dimensional and two-dimensional arrays, their initialization, properties, advantages, and disadvantages, as well as the concept of strings as null-terminated character arrays. Key points include the fixed size of arrays, random access capabilities, and the importance of the null character in strings.

Uploaded by

itzzmee664
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

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.

 Compared to the basic data type (int, float &


char) it is an aggregate or derived data type

 We can declare an array by specifying its


name, dimensions.

 Syntax of Array : data_type array_name


[size];
 Here, data_type define the base type of the
array, which is the type of each element in the
array. (int , float, char).
 array_name is any array name.
 array_size defines how many elements the
array will hold.
For example:
1. float mark[5];
Here, we declared an array, mark, of floating-point type. And its size is 5.
Meaning, it can hold 5 floating-point values.
 It's important to note that the size and type of an array cannot be changed
once it is declared.
2. int mark[5] = {19, 10, 8, 17, 9}; OR int mark[] = {19, 10, 8, 17, 9};
Why Do We Need Arrays?
Array stores an extensive collection of similar data types. We have
only three variables, and then we can easily store them using
separate names like int var1, int var2, and int var3, but what if we
have an extensive collection of these variables? Then, we cannot
assign separate names to each of them, as it would be tedious and
time-consuming.

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.

1. Array Initialization with Declaration

In this method, we initialize the array along with its declaration.


data_type array_name [size] = {value1, value2, ... valueN};
2. Initializing Array In C Without Size For example:
int idNum[7] = {1, 2, 3, 4, 5, 6, 7};
Let us initialise our array without a proper predefined size and elements inside. float fFloatNum[5] = {5.6, 5.7, 5.8,
This list contains the elements enclosed within curly braces {}. In this method, 5.9, 6.1};
the compiler automatically detects the size of the array. char chVowel[6] = {'a', 'e', 'i', 'o',
'u', '\0’};
data_type array_name[] = {1,2,3,4,5}; char chName[ ] = "Mr. Dracula";

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


compiler.
3. Declaring Array in C with Loops

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() {

int main() { // array declaration and initialization

// Declare the array int arr[5] = { 15, 25, 35, 45, 55 };


int arr[5]; // accessing element at index 2 i.e 3rd element
for (int i=0; i<5; i++)
printf("Element at arr[2]: %d\n", arr[2]);
arr[i] = i;
// accessing element at index 4 i.e last element

for (int i=0; i<5; i++) printf("Element at arr[4]: %d\n", arr[4]);


printf("%d\n", arr[i]); // accessing element at index 0 i.e first element
printf("Element at arr[0]: %d", arr[0]);
return 0;
return 0;
}
0 } Output
1 Element at arr[2]: 35
2 Element at arr[4]: 55
3
Element at arr[0]: 15
4
Types of
Arrays

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. Two Dimensional Arrays (2D Array)


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];

Example of 1-D Array:

Output
The array elements are : 10 20 30 40 50
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.

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];

size2: Size of the second dimension.

Example of 2-D Array :

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.

•arr_name: Name assigned to the array.

•m: Number of rows.

•n: Number of columns.

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 Traversal using for Loop:

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

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.

 The C String is stored as an array of characters. The difference between


a character array and a C string is that the string in C is terminated
with a unique character ‘\0’.

 Null character (‘\0’) used to indicate the termination of a string that


differs strings from normal character arrays.

Syntax : char string_name[size];

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’} ;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy