You are on page 1of 8

MODULE:3

ARRAYS AND STRINGS

ARRAYS
An array in C or be it in any programming language is a
collection of similar data items stored at contiguous memory
locations and elements can be accessed randomly using
indices of an array. 

FEATURES

1. It is a group of variables of similar data types referred


by single element.
2. It elements are stored in a contiguous memory
location.
3. The size of array should be mentioned while declaring
it.
4. Array elements are always counted from zero (0)
onward.
5. Array elements can be accessed using the position of
the element in the array.
6. Array can have one or more dimensions .
 

Array declaration in C

SYNTAX

data_type array_name[size];
Eg:int array[3];

Array declaration by initializing elements

SYNTAX

data_type array_name[size]={value1,value2….valuen};
Eg:int array[3]={1,2,3};

Advantages of an Array in C:  

1. Random access of elements using array index.


2. Use of fewer line of code as it creates a single array of
multiple elements.
3. Easy access to all the elements.
4. Traversal through the array becomes easy using a
single loop.
5. Sorting becomes easy as it can be accomplished by
writing fewer line of code.

Disadvantages of an Array in C:  

1. 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.
2. Insertion and deletion of elements can be costly since
the elements are needed to be managed in accordance
with the new memory allocation.

One-dimensional array example

1. #include<stdio.h>  
2. int main(){      
3. int i=0;    
4. int marks[5]={20,30,40,50,60};//declaration and initializatio
n of array    
5.  //traversal of array    
6. for(i=0;i<5;i++){      
7. printf("%d \n",marks[i]);    
8. }    
9. return 0;  
10. }    
Output
20
30
40
50
60
Two Dimensional Array in C

The two-dimensional array can be defined as an array of


arrays. The 2D array is organized as matrices which can be
represented as the collection of rows and columns. However,
2D arrays are created to implement a relational database
lookalike data structure. It provides ease of holding the bulk of
data at once which can be passed to any number of functions
wherever required.

Declaration of two dimensional Array in C


The syntax to declare the 2D array is given below.

data_type array_name[rows][columns];  
Consider the following example.

int twodimen[4][3];  

Initialization of 2D Array in C

In the 1D array, we don't need to specify the size of the array if


the declaration and initialization are being done simultaneously.
However, this will not work with 2D arrays. We will have to
define at least the second dimension of the array. The two-
dimensional array can be declared and defined in the following
way.

int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};  
Two-dimensional array example in C

1. #include<stdio.h>  
2. int main(){      
3. int i=0,j=0;    
4. int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};     
5. //traversing 2D array    
6. for(i=0;i<4;i++){    
7.  for(j=0;j<3;j++){    
8.    printf("arr[%d] [%d] = %d \n",i,j,arr[i][j]);    
9.  }//end of j    
10. }//end of i    
11. return 0;  
12. }    

Output
arr[0][0]=1
arr[0][1]=2
arr[0][2]=3
arr[1][0]=2
arr[1][1]=3
arr[1][2]=4
arr[2][0]=3
arr[2][1]=4
arr[2][2]=5
arr[3][0]=4
arr[3][1]=5
arr[3][2]=6

C Strings
The string can be defined as the one-dimensional array of
characters terminated by a null ('\0'). The character array or the
string is used to manipulate text such as word or sentences.
Each character in the array occupies one byte of memory, and
the last character must always be 0. The termination character
('\0') is important in a string since it is the only way to identify
where the string ends. When we define a string as char s[10],
the character s[10] is implicitly initialized with the null in the
memory.

Eg: char ch[10]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};  

C String Functions
There are many important string functions defined in "string.h"
library.

1.strlen() function

The strlen() function returns the length of the given string. It


doesn't count null character '\0'.

1. #include<stdio.h>  
2. #include <string.h>    
3. int main(){    
4. char ch[20]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};    
5.    printf("Length of string is: %d",strlen(ch));    
6.  return 0;    
7. }    

Output:
Length of string is: 10 Length of string is: 10
Length of string is:10

2.strcpy()
The strcpy(destination, source) function copies the source
string in destination.

1. #include<stdio.h>  
2. #include <string.h>    
3. int main(){    
4.  char ch[20]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};    
5.    char ch2[20];    
6.    strcpy(ch2,ch);    
7.    printf("Value of second string is: %s",ch2);    
8.  return 0;    
9. }    

Output:

You might also like