3. An array is a fixed-size sequenced collection of elements of same data type. ie., it
is a grouping of like-type. So in array a list of items can be given in one variable
name.
The general form of array declaration is
type variable_name[size]
Examples:
int dynami[50];
int trig[50];
int c[50];
float lab[40];
Array
4. Example:
Suppose we have to store prices of three different items in four shops:
Item 1 Item 2 Item 3
Shop 1 310 275 365
Shop 2 210 190 325
Shop 3 405 225 240
Shop4 260 300 380
5. C allows as to define such tables of item in single variable using two-
dimensional arrays such as
p[4][3]
Two-dimensional array are declared as follows:
Type array_name[row_size][column_size]
6. Structure is a mechanism for packing data of different data
type. A structure is a convenient tool for handling a group of related
data item.
Example:
Using structure we can represent a set of attributes, such as
student_name, roll_number and marks.
Structures in C
8. The keyword struct declares a structure to hold the details of four
data field, namely title, author, pages and price, these fields are called
structure elements or members. Note that each member may belong to
different data types. book-bank is the name of the structure which called
structure tag.
10. Declaring Structure Variables
After defining a structure we can declare variables of that type. A structure
variable declarations is similar to declaration of variables of any other data
types. It includes the following elements:
The keyword strut
The structure tag name.
List of variable names separated by commas.
A terminating semicolon.
Example:
struct book_bank, book1, book2, book3;
It declares book1, book2, book3 are variables of type struct book_bank.
11. Declaring Structure Variables
The complete variable definition and declaration is
struct book_bank
{
char title[20];
char author[15];
int page;
Float price;
};
struct book_bank, book1, book2, book3;
12. Declaring Structure Variables
It is also allowed to combine both variable definition and variable
declaration in one step as:
struct book_bank
{
char title[20];
char author[15];
int page;
float price;
} book1, book2, book3;
13. Access Structure Members
To access members of a structure, use the member operator dot
operator (.):
Example:
book1.price is the variable representing the price of book1 and it is treated
like any other ordinary variable.
14. Assigning values to the Structure variables
book.page=250;
book1.price=120.5
Strcpy(book1.title,”Algebra”);
Strcpy(book1.author, “Arumugam”);
15. Assigning values to the Structure variables
Reading data from keyboard
We can use scanf to give the values through the keyboard
Scanf(“%sn”,&book1.title);
Scanf(“%dn”, &book1.pages);
16. Example Program
struct personal
{
char name[20];
int day;
char month[10];
float salary;
};
main()
{
structure personal person;
printf(“Input Valuesn”);
scanf(“%s %d %s %f”, person.name,&person.day, &person.month,&person.salary);
printf(“Output Valuesn”);
printf (“%s %d %s %f”, person.name,person.day, person.month,person.salary);
}