structure in c

Upload: -

Post on 13-Oct-2015

19 views

Category:

Documents


0 download

DESCRIPTION

c

TRANSCRIPT

Slide 1

STRUCTURE IN C1P.Sharma,Dept. Of CSE,SMIT1Structures IN CStructures IN C

What is a structure ? Array is collection of items with identical data type C Supports a constructed data type known as structure. It is user-defined data type It wraps different data type It is way of extending new programming language

2P.Sharma,Dept. Of CSE,SMIT2Structures IN CStructures IN C

Definition:

- collection of variables under a single name.- variables can be of different typesa convenient way of grouping several pieces of related information together.

A structure is a collections of more than one variables with different data type like int, float & char

Example :struct record{char name[25];int sci, eng, maths;float per;};

A structure is a collections of more than one variables with different data type like int, float & char

P.Sharma,Dept. Of CSE,SMITDefining astructure General form or syntax:-struct tag_nameStructure tag name{datatype variable;datatype variable;datatype variable; //structure elements /members------------------------------------------} ;

4P.Sharma,Dept. Of CSE,SMITDefining structure variablesMethod 1:- General form or syntaxstruct tag_nameStructure tag name{datatype variable;datatype variable;datatype variable; //structure elements------------------------------------------} list of variables;//Structure Variables

5P.Sharma,Dept. Of CSE,SMITDefining structure variablesMethod2: General form or syntaxstruct tag_name{datatype member1;datatype member2;datatype member3 ------- -------- };void main(){ struct tag_name var_name; //Structure Variable}6P.Sharma,Dept. Of CSE,SMIT

Accessing Structure Members The link between a member and a variable is established using the member operator . Which is known as dot operator or period operator. Example:Book.priceBook.price is the variable representing the price of book and can be treated like any other ordinary variable. We can use scanf statement to assign values like:- syntax:- Tag_name.member ;Example:- scanf(%s,book.file); scanf(%d,&book.pages);

7P.Sharma,Dept. Of CSE,SMIT

Accessing Structure Members We can assign variables to the members of book :-

strcpy(book.title,Programming ANSI C);strcpy(book.author,Balagurusamy);

book.pages=250;book.price=28.50;

8P.Sharma,Dept. Of CSE,SMITStructure memberStructure name#include #include struct student { int id; char name[20]; //array within a structure float percentage;};void main() { struct student record ; //method 2 record.id=1; strcpy(record.name, "Raju"); record.percentage = 86.5; printf(" Id is: %d \n", record.id); printf(" Name is: %s \n", record.name); printf(" Percentage is: %f \n", record.percentage); getch( );}OutputId is: 1Name is: RajuPercentage is: 86.5000009P.Sharma,Dept. Of CSE,SMITExample of a structure#include #include struct student {int id;char name[20]; //array within a structurefloat percentage;} record; //method 1int main() {record.id=1;strcpy(record.name, "Raju");record.percentage = 86.5;printf(" Id is: %d \n", record.id);printf(" Name is: %s \n", record.name);printf(" Percentage is: %f \n", record.percentage);getch( );}10P.Sharma,Dept. Of CSE,SMITOutputId is: 1Name is: RajuPercentage is: 86.500000Example of a structureStructure member vs variable

struct book_bank{char title[20];char author[15]; int pages;float price;};

void main(){struct book_bank x; //x is structure variableint i,j;clrscr();--------------------------------------------- getch();

}

11P.Sharma,Dept. Of CSE,SMITStructure member

Guidelines for declaring structurevariables :-We can declare a structure variable only when a structure is defined.Declaration of structure variable is similar to the declaration of variable of any other data type.It includes following points: - The keyword struct - The structure tag name - List of variable names separated by commas. - A terminating semicolon For example : struct book_bank book1, book2, book3;

12P.Sharma,Dept. Of CSE,SMITMembers of the structure are themselves not a variable.They do not occupy any memory and till associate with structure variable.The memory for structure member is not allocated when they are declared, memory is allocated only when the structure variable is declared.

A structure is usually defines before main along with macro definitions.

In such cases the structure assumes global status and all the functions can access the structure. Guidelines for using Structures in C13P.Sharma,Dept. Of CSE,SMITStructure Initialization We can initialize structure when we declare them.A structure variable can be initialized at compile time.struct book_bank{char title[20]; char author[15]; int pages; float price;} book1={ANSI C,Balaguruswamy,430,200.0}; // structure initialization

Note : structure members cannot be initialized within the structure definition.14P.Sharma,Dept. Of CSE,SMITInitialization of these variables cannot be done here .Compile time Initialization struct book_bank{char title[20]; char author[15]; int pages; float price;}; void main(){ struct book_bank book1 = {ANSI C, Balaguruswamy,430,200.0}; // structure initialization}Note : structure members cannot be initialized within the structure definition.

P.Sharma,Dept. Of CSE,SMIT15

Structure Initialization C language does not permit the initialization of individual structure members within the template.At compile time initialization of structure variable must have the following elements1. The Keyword struct2. The structure tag name3. The name of the variable to be declared4. The assignment operator5. A set of values for the members of the structure variable, separated by commas and enclosed in braces6. A terminating semicolon

16P.Sharma,Dept. Of CSE,SMIT

Rules for initializing structure We cannot initialize individual members inside the structure templateThe order of values enclosed in braces must match the order of members in the structure definitionIt is permitted to have partial initilization. We can initialize only the first few members and leave the remaining blank. The uninitialized members should be only at the end of the list.The uninitialized members will be assigned default values as follows :- - Zero for integer and floating point numbers - \0 for characters and strings

17P.Sharma,Dept. Of CSE,SMITTwo variables of the same structure type can be copied the same way as ordinary variables book1 = book2; //allowedThere is no way to compare entire structure, we can only compare element by element/* this is not allowed */if(book1==book2) /* this is not allowed */;

/* where as we can compare element by element */if(book1.pages == book2.pages); //alowed

Copying and Comparing Structure Variables18P.Sharma,Dept. Of CSE,SMIT ARRAY OF STRUCTURE

Like array of int, float or char, we can also have array of structureWe can use single-dimensional or multi-dimensional arrays Examplestruct student{char name[20];char city[15]; int subject[3]; float per;} stud[10]; // array of structure

Subject contains three elements :-subject[0], subject[1] and subject[2]. These elements can be accessed as followed :-stud[1].subject[2]; // This will refer to the marks of third subject of second studentAccessing name of 2nd student :-stud[1].name;19P.Sharma,Dept. Of CSE,SMIT#include #include struct student { int id; char name[30]; float percentage;};void main() { int i; struct student record[2]; // 1st student's record record[0].id=1; strcpy(record[0].name, "Raju"); record[0].percentage = 86.5; // 2nd student's record record[1].id=2; strcpy(record[1].name, "Surendren"); record[1].percentage = 90.5; // 3rd student's record record[2].id=3; strcpy(record[2].name, "Thiyagu"); record[2].percentage = 81.5; for(i=0; i