ArrAys And structure
Array :- Array is the collection of similar data type or collection of similar entry store in
continuous memory location.
Advantages :- Array variable can store more than one value at the time where other
variable can store one value at a time.
Types of array:-
1. One dimensional Array:-A one dimensional array contains a series of elements
of the same datatype.
Syntax:-
Data-name array name[size of array];
Example
int a[5];
float a[5];
2. Two dimensional array :- two dimensional array is also known as matrix.
Syntax :-
Data-type array name[row][column];
Example :-
int a[2][3];
Total number of element or size of array is = row*column=2*3=6.
Intialization of 2-d array:
Two dimensional array can be in initialized in a way similar to that one dimensional
array.
int mat[4][3]={1,2,3,4,5,6,7,8,9,10,11,12};
while initialization we can group the element row wise using inner braces.
int mat[4][3]={{1,2,3},{4,5,6},{7,8,9},{10,11,12}};
Sring:- array of character is called as string. it is always terminated by null
character. string is a one dimensional array of character.
Char name[]={‘j’,’o’,’h’,’n’,’\0’};
String constant:-A sting constant is a set of character that enclose with in double
quotes and it is also called a literal.
Char crr[20]= ”Taj Mahal”;
Operation on array:-
Scanf(“%d”,&a[3]);
An integer value will be read from user and store at location in the array a.
Scanf(“%f”,&b[i]);
An float value will be read from user and Store at [i] location in array b.
Structure :- it is the collection of dissimar data type or heterogeneous data type
group together
Structure declaration
Struct tagname
Data type member 1;
Data type member 2;
Data type member 3;
-------------------------
Data type member n;
};
Initialization of structure variable :-
Like primary variable structure variable can also be initialized when they are
declared.
Struct student
Int age,roll;
Char name[20];
} struct student s1={16,101,”sona”};
Struct student s2={17,102,”rupa”};
Accessing structure element:-
Element of structure are store in continuous memory location.
Structure variable;
S1.name[];
S1.roll;
S1.age;
Array of structure :- when database of any element is used in huge amount ,we
prefer array of structure.
#include<stdio.h>
#include<conio.h>
Struct student
Char name[30];
Char branch[25];
Int roll;
Void main()
Struct student s[200];
Int I;
S[i].roll=i+1;
Printf(“\nEnter information of students:”);
For(i=0;i<200;i++)
Printf(“\nEnter the roll no:%d\n”,s[i].roll);
Printf(“\nEnter the name:”);
Scanf(“%s”,s[i].name);
Printf(“\nEnter the branch:”);
Scanf(“%s”,s[i].branch);
Printf(“\n”);
Printf(“\nDisplaying information of students:\n\n”);
For(i=0;i<200;i++)
Printf(“\n\nInformation for roll no%d:\n”,i+1);
Printf(“\nName:”);
Printf(“\nBranch:”);
getch();
}
Type definition :- The typedef is a keyword that is used to provide existing data
types with a new name.
Syntax :-
Typedef type identifier;
Example :-
Typedef int my_int;
Enumerated data type:-is a user-defined data type in C programming. It enables
developers to establish a collection of named constants, known as enumerators.
Syntax :- enum gender
Male, Female,other
};
String library function :- There are several string library functions used to
manipulate string and the prototypes for these functions are in header file
“string.h”.
Strlen():-this function return the length of the string.
Char alphabet[]={“A,b,c,d,e,f,g”};
Printf(“%d”,strlen(alphabet);
Strcmp():-this function is used to compare two string. If the two string match ,
strcmp() written a value 0 otherwise it is written a non zero value.
Char s1[]={“Hello”};
Char s2[]={“Hello”};
Char s3[]={“Hi”};
Printf(“%d”, strcmp(s1,s2));
Printf(“%d”, strcmp(s1,s3));
Strcpy() :- this function is used to copy one string to another string.
Char s1[]={“Hello”};
Char s2[]={“ “ };
Strcpy(s2,s1);
Printf”%s”,s2);
Strcat() :- this function is used to combine a two string.
Char s1[]={“ Hello”};
Char s2[]={“world”};
Strcat(s1,s2)
Printf(“%s”,s1);