Sie sind auf Seite 1von 11

A structure is a user defined data type.

We know that arrays can be used to represent a group of data items that belong to the same type, such as int or float. However we cannot use an array if we want to represent a collection of data items of different types using a single name. A structure is a convenient tool for handling a group of logically related data items. The syntax of structure declaration is struct structure_name type element !" type element #" $$$$$.. type element n" %" &n structure declaration the keyword struct appears first, this followed by structure name. The member of structure should be enclosed between a pair of braces and it defines one by one each ending with a semicolon. &t can also be array of structure. There is an enclosing brace at the end of declaration and it end with a semicolon. We can declare structure variables as follows struct structure_name var!,var#,$..,var n" 'or (xample) To store the names, roll number and total mark of a student you can declare * variables. To store this data for more than one student * separate arrays may be declared. Another choice is to make a structure. +o memory is allocated when a structure is declared. &t ,ust defines the -form. of the structure. When a variable is made then memory is allocated. This is e/uivalent to saying that there0s no memory for -int. , but when we declare an integer that is. int var" only then memory is allocated. The structure for the above1mentioned case will look like

struct student int rollno" char name2#34" float totalmark" %" We can now declare structure variables stud!, stud# as follows struct student stud!,stud#" Thus, the stud! and stud# are structure variables of type student. The above structure can hold information of # students. &t is possible to combine the declaration of structure combination with that of the structure variables, as shown below. struct structure_name type element !" type element #" $$$$$.. type element n" %var!,var#,$,varn" The following single declaration is e/uivalent to the two declaration presented in the previous example. struct student int rollno" char name2#34" float totalmark" % stud!, stud#" The different variable types stored in a structure are called its members. The structure member can be accessed by using a dot 5.6 operator, so the dot operator is known as structure member operator.

(xample) &n the above example stud! is a structure variable of type student. To access the member name, we would write stud!.name 7imilarly, stud!8s rollno and stud!8s totalmark can be accessed by writing stud!.rollno stud!.totalmark Initializing Structure Members 7tructure members can be initiali9ed at declaration. This much the same manner as the element of an array" the initial value must appear in the order in which they will be assigned to their corresponding structure members,enclosed in braces and seperated by commas .The general form is struct stucture_name var: val!,val#,val*$..%" (xample) ;include <stdio.h= ;include<conio.h= int main56 struct student char >name" int rollno" float totalmark" %" struct student stud!: ?Ashraf?,!,@A%" struct student stud*: ?Bahul?,*,@C%" struct student stud#: ?Dineeth?,#,@@%" clrscr56" printf5?7TEF(+T7 F(TA&G7)HnBoll number)IdHnHn+ame)IsHnHnTotel mark)I.#fHn?,stud!.rollno,stud!.name,stud!.totalmark6" And

printf5?HnBoll number)IdHnHn+ame)IsHnHnTotel mark) I.#fHn?,stud#.rollno,stud#.name,stud#.totalmark6" printf5?HnBoll number)IdHnHn+ame)IsHnHnTotel mark) I.#fHn?,stud*.rollno,stud*.name,stud*.totalmark6" getch56" return J" % Array of structures: &t is possible to store a structure has an array element. i.e., an array in which each element is a structure. Kust as arrays of any basic type of variable are allowed, so are arrays of a given type of structure. Although a structure contains many different types, the compiler never gets to know this information because it is hidden away inside a sealed structure capsule, so it can believe that all the elements in the array have the same type, even though that type is itself made up of lots of different types. The declaration statement is given below. struct struct_name type element !" type element #" $$$$$.. type element n" %array name2si9e4" (xample) struct student int rollno" char name2#34" float totalmark" % stud2!JJ4"

&n this declaration stud is a !JJ1element array of structures. Hence, each element of stud is a separate structure of type student. An array of structure can be assigned initial values ,ust as any other array. 7o the above structure can hold information of !JJ students. program) ;include <stdio.h= ;include <conio.h= int main56 struct student int rollno" char name2#34" int totalmark" %stud2!JJ4" int n,i" clrscr56" printf5?(nter total number of studentsHnHn?6" scanf5?Id?,Ln6" for5i:J"i<n"iMM6 printf5?(nter details of Id1th studentHn?,iM!6" printf5?+ame)Hn?6" scanf5?Is?,Lstud2i4.name6" printf5?Boll number)Hn?6" scanf5?Id?,Lstud2i4.rollno6" printf5?Total mark)Hn?6" scanf5?Id?,Lstud2i4.totalmark6" % printf5?7TEF(+T7 F(TA&G7)Hn?6" for5i:J"i<n"iMM6

printf5?HnBoll number)IdHn?,stud2i4.rollno6" printf5?+ame)IsHn?,stud2i4.name6" printf5?Totel mark)IdHn?,stud2i4.totalmark6" % getch56" return J" % Structure as structure member: A structure can have one or more of its member as another structure, but a structure cannot be member to itself when a structure is used as structure member. &n such situation, the declaration of the embedded structure must appear before the declaration of the outer structure. 'or example ;include <stdio.h= ;include <conio.h= int main56 struct dob int day" int month" int year" %" struct student struct dob d" int rollno" char name2#34" int totalmark" %stud2#34" int n,i" clrscr56"

printf5?(nter total number of studentsHnHn?6" scanf5?Id?,Ln6" for(i=0;i<n;i++) { rintf(!"n#nter $etails of %$&t' stu$ent"n"n!(i+)); rintf(!"n*ame:"n!); scanf(!%s!(+stu$,i-.name); rintf(!"n/oll number:"n!); scanf(!%$!(+stu$,i-.rollno); rintf(!"n0otal mar1:"n!); scanf(!%$!(+stu$,i-.totalmar1); rintf(!"n2ate of birt' (3ormat:0) 04 50)0):"n!); scanf(!%$%$ %$!(+stu$,i-.$.$ay(+stu$,i-.$.mont'(+stu$,i-.$.year); 6 printf5?7TEF(+T7 F(TA&G7)Hn?6" for5i:J"i<n"iMM6 printf5?HnHnBoll number)IdHnHn?,stud2i4.rollno6" printf5?+ame)IsHnHn?,stud2i4.name6" printf5?Totel mark)IdHnHn?,stud2i4.totalmark6" printf5?Fate of birth)Id N Id N Id HnHn?,stud2i4.d.day,stud2i4.d.month,stud2i4.d.year6" % getch56" return J" % 7nion 7nion is a data type with two or more member similar to structure but in this case all the members share a common memory location. The si9e of the union corresponds to the length of the largest member. 7ince the member share a common location they have the same starting address.

The real purpose of unions is to prevent memory fragmentation by arranging for a standard si9e for data in the memory. Oy having a standard data si9e we can guarantee that any hole left when dynamically allocated memory is freed will always be reusable by another instance of the same type of union. This is a natural strategy in system programming where many instances of different kinds of variables with a related purpose and stored dynamically. A union is declared in the same way as a structure.The syntax of union declaration is union union_name type element !" type element #" $$$$$.. type element n" %" This declares a type template. Dariables are then declared as) union union_name x,y,9" 'or example, the following code declares a union data type called 7tudent and a union variable called stud) union student int rollno" float totalmark" %" union student stud" &t is possible to combine the declaration of union combination with that of the union variables, as shown below. union union_name type element !" type element #"

$$$$$.. type element n" %var!,var#,$,varn" The following single declaration is e/uivalent to the two declaration presented in the previous example. union student int rollno" float totalmark" %x,y,9" (xercise) Pompare structure and Enion 7tructure)

Enion)

#include <stdio.h> #include<conio.h> int main() { union student { char *name; int rollno; float totalmark; }stud1; // union student stud1={" shraf"!1!"#}; stud1.name=" shraf"; $rintf("%&'()*&% ()& +,%-.nname-/s"!stud1.name); stud1.rollno=1; $rintf(".n0oll num1er-/d"!stud1.rollno); stud1.totalmark=23.45;

$rintf(".ntotalmark-/f"!stud1.totalmark);

6hat is the main difference 1et7een %&0'8&'0) and '*+9*:

All the members of the structure can be accessed at once,where as in an union only one member can be used at a time. Another important difference is in the size allocated to a structure and an union. for eg: struct example { int integer; float floating_numbers; } the size allocated here is sizeof int!"sizeof float!; where as in an union union example { int integer; float floating_numbers; } size allocated is the size of the highest member. so size is#sizeof float!;

Das könnte Ihnen auch gefallen