Pages

Array


What is array ?
                            Arrays are the groups of similar data types. It is used to store a large amount of data. The array elements are stored in contiguous memory locations. In other words, if an array is declared to be of an int type, it can’t contain elements that are not of int type.
How to declare an Array:
                                         Data Type       variable name [size];

                                            Example: -          int ary [5];

10
20
30
40
50
            ary[0]                  ary[1]                  ary[2]             ary[3]                  ary[4]

In the above example array is variables name which holds 5 elements which have a same name, same data type but with different memory locations. As the memory is allocated sequentially, data can be easily accessed by increment or decrement by single variable. Arrays always start with index value zero.

Memory allocation to an array
                                                        As we know array allocates continuous memory to its elements. It allocates memory in sequence order one after the other in forward direction. It allocates memory address in increasing order.
As shown in the example
                                                            int a[5];
Here we declare an array of integer type which allocates 10 contiguous bytes memory to the variable a.

            307                             309                 311                             313                        315





            a[0]                        a[1]                            a[2]                             a[3]                  a[4]
As shown above in memory graph. Each memory has a memory address like
307,309,311,315 and so on. Each memory block has 2 bytes memory space.
It means an array allocates contiguous bytes.

Type of Array 

One dimensional Array
Two  dimensional Array

 One dimensional Array  :   A list of items  has only one subscript and such  a list is called One dimensional Array  for eg

                            datatype  variable  name  [value ];
                             int  a [10];

Two dimensional Array :-- Two dimensional Array means an array of array,which has two sub script .The first sub script represents the rows and second subsript represent the columns.it is also know as matrix .The row and column size of the two dimension array may or may not be same .for eg 
                            datatype  variable  name  [row  ][column];
                             int  a [2][2]; 

3-Dimensional array  :-we can also create three dimensional array same as 2-D array. But it has three sub script .it is also know as multidimensional array
                              
                               int a[2][2][2];



String          it means group of characters .Character array is know as string  in C language.A character array  can store the name of a person ,his address ,etc.
  C  language  has  no data type ,which can store  the whole name of a person  Charanter array means an array that can store more then one character .A string is a one dimensional character array terminated by a NULL  character  .String is always  is enclosed in double quotes. its format  specified  is %s .

Syntax  :

                       datatype variable [size];
                        char a [22];


       
   String library functions         String library functions   are  the functions which are used  regularly and stored  in library file and whenever  these funouctions are  needed  you need to include the required header file in your program in top .  For eg
      
                                                            #include <string.h>

Inbuilt function:  There are seven inbuilt functions to perform different operations strings .These functions are as follows :

strlen( )   This function is used to count the character in a  string .it calculates the length of  the string

Syntax   strlen (array  variable );

 Programe
  #include <string.h>
#include<conion.h>
#include<stdio.h>
main()
{
char a[30];
int  I;
printf(“enter the string:--“);
scanf(“%s”&a);
i=strlen(a);
printf(“the character lenth is %d “, i);
getch();
}


strcpy()   This function is copies the contents of one string into another string .

Syntax     strcpy( target, source);

strcat()   This function is used to concatenate the source string at the end  of the target string
Syntax      strcat(target , source to add);

strcmp()  This function  compares two string to  find out whether  they are same of different .The two string are compared character by character until there is a mismatch or end of the string.

Syntax     strcmp(string1, string2);

strrev()    This function is used to reverse the string .This function  also contains a single argument.

Syntax     strrev ( variable);

Strlwr()  This function is used to convert  the  string  to lower case .it contain the single argument  .
Syntax  strlwr(  variable);

Strupr()  This function is used to convert  the  string  to Upper case .it contain the single argument  .
Syntax  strupr(  variable);






0 comments: