Upcoming SlideShare
×

Lecture18 structurein c.ppt

4,663 views

Published on

structure in C

1 Comment
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• blessing_11111@yahoo.com

My name is Blessing
i am a young lady with a kind and open heart,
I enjoy my life,but life can't be complete if you don't have a person to share it
with. blessing_11111@yahoo.com

Hoping To Hear From You
Yours Blessing

Are you sure you want to  Yes  No
• Be the first to like this

Views
Total views
4,663
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
297
1
Likes
0
Embeds 0
No embeds

No notes for slide

Lecture18 structurein c.ppt

1. 1. Lecturer, B V Patel Inst. of BMC & IT, GopalVidyanagar STRUCTURE IN ‘C’Prakash KhairePrakash Khaire Lecturer, B V Patel Inst. of BMC & IT, GopalVidyanagar
2. 2. Structure in C●Array is collection of items with identical data type●C Supports a constructed data type known asstructure●It is user-defined data type●It wraps different data type●It is way of extending new programming language Prakash Prakash Khaire Lecturer, B V Patel Inst. of BMC & BMC & IT, GopalVidyanagar Lecturer, B V Patel Inst. of IT, GopalVidyanagar Khaire
3. 3. Definition - Structure●A structure is collections of more than one variables withdifferent data type like int, float & charExample :char name[25];int sci, eng, maths;float per;PrakashPrakash Khaire Lecturer, B V Patel Inst. of BMC & BMC & IT, GopalVidyanagar Lecturer, B V Patel Inst. of IT, GopalVidyanagarKhaire
4. 4. Defining a structure●General form or syntaxstruct tag_name Structure tag name{ datatype variable; datatype variable; datatype variable; structure elements --------------------- ---------------------} list of variables; Structure VariablesPrakashPrakash Khaire Lecturer, B V Patel Inst. of BMC & BMC & IT, GopalVidyanagar Lecturer, B V Patel Inst. of IT, GopalVidyanagarKhaire
5. 5. Examplestruct book_bank void main(){ { char title[20]; struct book_bank x,y, MyBooks[5]; int i,j; char author[15]; clrscr(); int pages; --------------- float price; --------------- ---------------}; getch(); }PrakashPrakash Khaire Lecturer, B V Patel Inst. of BMC & BMC & IT, GopalVidyanagar Lecturer, B V Patel Inst. of IT, GopalVidyanagarKhaire
6. 6. Declaring structure variables 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; ●PrakashPrakash Khaire Lecturer, B V Patel Inst. of BMC & BMC & IT, GopalVidyanagar Lecturer, B V Patel Inst. of IT, GopalVidyanagarKhaire
7. 7. Structures in C●Members of the structure are themselves not avariable. They do not occupy any memory andtill associate with structure variable as book.●A structure is usually defines before main alongwith macro definitions.● In such cases the structure assumes globalstatus and all the functions can access thestructure.PrakashPrakash Khaire Lecturer, B V Patel Inst. of BMC & BMC & IT, GopalVidyanagar Lecturer, B V Patel Inst. of IT, GopalVidyanagarKhaire
8. 8. Accessing Structure MembersThe link between a member and a variable is established using the●member operator ‘.’ Which is known as dot operator or period operator.For example:Book1.priceIs the variable representing the price of book1 and can be treated like any●other ordinary variable. We can use scanf statement to assign values likescanf(“%s”,book1.file);scanf(“%d”,&book1.pages);PrakashPrakash Khaire Lecturer, B V Patel Inst. of BMC & BMC & IT, GopalVidyanagar Lecturer, B V Patel Inst. of IT, GopalVidyanagarKhaire
9. 9. Accessing Structure MembersWe can assign variables to the members of book1●strcpy(book1.title,”Programming ANSI C”);strcpy(book1.author,”Balagurusamy”);book1.pages=250;book1.price=28.50;PrakashPrakash Khaire Lecturer, B V Patel Inst. of BMC & BMC & IT, GopalVidyanagar Lecturer, B V Patel Inst. of IT, GopalVidyanagarKhaire
10. 10. Structure InitializationLike other data type 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}; or struct book_bank book1 = {“ANSI C”,”Balaguruswamy”,430,200.0};PrakashPrakash Khaire Lecturer, B V Patel Inst. of BMC & BMC & IT, GopalVidyanagar Lecturer, B V Patel Inst. of IT, GopalVidyanagarKhaire
11. 11. Structure InitializationC 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 inbraces6. A terminating semicolonPrakashPrakash Khaire Lecturer, B V Patel Inst. of BMC & BMC & IT, GopalVidyanagar Lecturer, B V Patel Inst. of IT, GopalVidyanagarKhaire
12. 12. Rules for initializing structure●We cannot initialize individual members inside the structure template●The order of values enclosed in braces must match the order ofmembers in the structure definition●It is permitted to have partial initilization. We can initialize only the firstfew members and leave the remaining blank. The uninitializedmembers 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 stringsPrakashPrakash Khaire Lecturer, B V Patel Inst. of BMC & BMC & IT, GopalVidyanagar Lecturer, B V Patel Inst. of IT, GopalVidyanagarKhaire
13. 13. Copying and Comparing StructureVariablesTwo variables of the same structure type can be●copied the same way as ordinary variablesbook1 = book2;There 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);PrakashPrakash Khaire Lecturer, B V Patel Inst. of BMC & BMC & IT, GopalVidyanagar Lecturer, B V Patel Inst. of IT, GopalVidyanagarKhaire
14. 14. Structure in C Entire structures (not just pointers to structures) ●may be passed as function arguments, assigned tovariables, etc. Interestingly, they cannot be compared using == ● Unlike arrays, structures must be defined before, its ●variable are usedPrakashPrakash Khaire Lecturer, B V Patel Inst. of BMC & BMC & IT, GopalVidyanagar Lecturer, B V Patel Inst. of IT, GopalVidyanagarKhaire
15. 15. Array of structure●Like array of int, float or char, we can also have array of structure●We can use single-dimensional or multi-dimensional arrays●Examplestruct student{ Subject contains three elements, char name[20]; subject[0], subject[1] and subject[2]. char city[15]; These elements can be accessed as int subject[3]; followed float per; stud[1].subject[2]}stud[10]; This will refer to the marks of third subject of second studentPrakash Khaire Lecturer, B V Patel Inst. of BMC & IT, GopalVidyanagar
16. 16. Structures within structureStructure within structure is known as nested structure● struct date struct company { // members of structure   { int day; char name[20]; int month; long int employee_id; int year; char sex[5]; }; int age; struct company { struct char name[20]; { long int employee_id; int day; char sex[5]; int month; int age; int year; struct date dob; }dob; }; }employee;Prakash Khaire Lecturer, B V Patel Inst. of BMC & IT, GopalVidyanagar
17. 17. Structure within structureAn inner most member in a nested structure●can be accessed by chaining all theconcerned structure variables with themember using dot operator.Example●employee.dob.dayemployee.dob.monthemployee.dob.yearPrakash Khaire Lecturer, B V Patel Inst. of BMC & IT, GopalVidyanagar
18. 18. typedef DeclarationIt stands for "type definition"●keyword allows the programmer to create new●names for types such as int or, moreIt refers to definition of data types which can be●used by the users to declare their own datadefinitions names.PrakashPrakash Khaire Lecturer, B V Patel Inst. of BMC & BMC & IT, GopalVidyanagar Lecturer, B V Patel Inst. of IT, GopalVidyanagarKhaire
19. 19. syntax●So, how do you actually declare a typedef? All you must do is providethe old type name followed by the type that should represent itthroughout the code. Heres how you would declare size_t to be anunsigned integer● typedef unsigned int size_t;PrakashPrakash Khaire Lecturer, B V Patel Inst. of BMC & BMC & IT, GopalVidyanagar Lecturer, B V Patel Inst. of IT, GopalVidyanagarKhaire