Structure Definition: General Format: Struct Tag - Name (Data Type Member1 Data Type Member2 )
Structures allow storing different data types together in a single variable. They group related data and can contain many members of different types. An array of structures can store multiple structured records together. Structures can also be nested, with one structure defined as a member of another structure. This allows building complex nested records. Unions are similar to structures but all members in a union share the same memory space, making unions more memory efficient than structures.
Download as DOC, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
40 views
Structure Definition: General Format: Struct Tag - Name (Data Type Member1 Data Type Member2 )
Structures allow storing different data types together in a single variable. They group related data and can contain many members of different types. An array of structures can store multiple structured records together. Structures can also be nested, with one structure defined as a member of another structure. This allows building complex nested records. Unions are similar to structures but all members in a union share the same memory space, making unions more memory efficient than structures.
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10
Arrays are used to store large set of data and manipulate them but the
disadvantage is that all the elements stored in an array are to be of
the same data type. If we need to use a collection of different data type items it is not possible using an array. When we require using a collection of different data items of different data types we can use a structure. Structure is a method of packing data of different types. A structure is a convenient method of handling a group of related data items of different data types. structure definition: general format: struct tag_name { data type member1; data type member2;
} Example: struct lib_books { char title[20; char author[1!; int pages; float price; }; the keyword struct declares a structure to holds the details of four fields namely title, author pages and price. These are members of the structures. ach member may belong to different or same data type. The tag name can be used to define ob!ects that have the tag names structure. The structure we !ust declared is not a variable by itself but a template for the structure. We can declare structure variables using the tag name any where in the program. "or e#ample the statement, struct lib_books book1"book2"book#; declares book$,book%,book& as variables of type struct lib'books each declaration has four elements of the structure lib'books. The complete structure declaration might look like this struct lib_books { char title[20; char author[1!; int pages; float price; }; struct lib_books" book1" book2" book#; structures do not occupy any memory until it is associated with the structure variable such as book$. the template is terminated with a semicolon. While the entire declaration is considered as a statement, each member is declared independently for its name and type in a separate statement inside the template. The tag name such as lib'books can be used to declare structure variables of its data type later in the program. We can also combine both template declaration and variables declaration in one statement, the declaration struct lib_books { char title[20; char author[1!; int pages; float price; } book1"book2"book#; is $alid% &he use of tag name is optional for e'ample struct {
} book$, book%, book& declares book$,book%,book& as structure variables representing & books but does not include a tag name for use in the declaration. 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. Giving values to members: As mentioned earlier the members themselves are not variables they should be linked to structure variables in order to make them meaningful members. The link between a member and a variable is established using the member operator (.) Which is known as dot operator or period operator. For example: (ook1%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 scanf)*+s,"book1%file-; scanf)*+d,". book1%pages-; *r we can assign variables to the members of book$ strcpy)book1%title",basic,-; strcpy)book1%author",(alagurusamy,-; book1%pages/2!0; book1%price/20%!0; 12 3'ample program for using a structure21 4include5 stdio%h 6 $oid main)- { int id_no; char name[20; char address[20; char combination[#; int age; }ne7student; printf)*3nter the student information,-; printf)*8o7 3nter the student id_no,-; scanf)*+d,".ne7student%id_no-; printf)*3nter the name of the student,-; scanf)*+s,".ne7 student%name-; printf)*3nter the address of the student,-; scanf)*+s,".ne7 student%address-; printf)*3nter the cmbination of the student,-; scanf)*+d,".ne7 student%combination-; printf)*3nter the age of the student,-; scanf)*+d,".ne7 student%age-; printf)*9tudent information:n,-; printf)*student id_number/+d:n,"ne7student%id_no-; printf)*student name/+s:n,"ne7student%name-; printf)*student ;ddress/+s:n,"ne7student%address-; printf)*students combination/ +s:n,"ne7student%combination-; printf)*;ge of student/+d:n,"ne7student%age-; } Initializing structure: +ike other data type we can initiali,e structure when we declare them. As for initali,ation goes structure obeys the same set of rules as arrays we initali,e the fields of a structure by the following structure declaration with a list containing values for weach fileds as with arrays these values must be evaluate at compile time. Example: Struct student newstudent - $%&./, 0kapildev1 02es college13 04se13 $53 63 this initiali,es the id'no field to $%&./, the name field to 0kapildev1, the address field to 0pes college1 the field combination to 0cse1 and the age field to $5. Functions and structures: We can pass structures as arguments to functions. 7nlike array names however, which always point to the start of the array, structure names are not pointers. As a result, when we change structure parameter inside a function, we don)t effect its corresponding argument. 2assing structure to elements to functions8 A structure may be passed into a function as individual member or a separate variable. A program e#ample to display the contents of a structure passing the individual elements to a function is shown below. 4 include 5 stdio%h 6 $oid main)- { int emp_id; char name[2!; char department[10; float salary; }; static struct emp1/{12!",sampath,",operator,"<!00%00}; 12 pass only emp_id and name to display function21 display)emp1%emp_id"emp1%name-; } 12 function to display structure $ariables21 display)e_no"e_name- int e_no"e_name; { printf)*+d+s,"e_no"e_name-; in the declaration of structure type, emp'id and name have been declared as integer and character array. When we call the function display9: using display9emp$.emp'id,emp$.name:3 we are sending the emp'id and name to function display9;:3 it can be immediately reali,ed that to pass individual elements would become more tedious as the number of structure elements go on increasing a better way would be to pass the entire structure variable at a time. Passing entire function to functions: In case of structures having to having numerous structure elements passing these individual elements would be a tedious task. In such cases we may pass whole structure to a function as shown below8 4 include stdio%h6 { int emp_id; char name[2!; char department[10; float salary; }; $oid main)- { static struct employee emp1/ { 12" *sadanand," *computer," <!00%00 }; 12sending entire employee structure21 display)emp1-; } 12function to pass entire structure $ariable21 display)empf- struct employee empf { printf)*+d+s"+s"+f," empf%empid"empf%name"empf%department"empf%salary-; } Arrays of structure: It is possible to define a array of structures for e#ample if we are maintaining information of all the students in the college and if $;; students are studying in the college. We need to use an array than single variables. We can define an array of structures as shown in the following e#ample8 structure information { int id_no; char name[20; char address[20; char combination[#; int age; } student[100; An array of structures can be assigned initial values !ust as any other array can. <emember that each element is a structure that must be assigned corresponding initial values as illustrated below. 4include5 stdio%h 6 { struct info { int id_no; char name[20; char address[20; char combination[#; int age; } struct info std[100; int ="n; printf)*3nter the number of students,-; scanf)*+d,".n-; printf)* 3nter =d_no"name address combination age:m,-; for)=/0;= 5 n;=>>- scanf)+d+s+s+s +d,".std[=%id_no"std[=%name"std[=%address"std[=%com bination".std[=%age-; printf)*:n 9tudent information,-; for )=/0;=5 n;=>>- printf)*+d+s+s+s+d:n," ,"std[=%id_no"std[=%name"std[=%address"std[=%combin ation"std[=%age-; } Structure witin a structure: A structure may be defined as a member of another structure. In such structures the declaration of the embedded structure must appear before the declarations of other structures. struct date { int day; int month; int year; }; struct student { int id_no; char name[20; char address[20; char combination[#; int age; structure date def; structure date doa; }oldstudent" ne7student; the sturucture student constains another structure date as its one of its members. !nion: 7nions like structure contain members whose individual data types may differ from one another. =owever the members that compose a union all share the same storage area within the computers memory where as each member within a structure is assigned its own unique storage area. Thus unions are used to observe memory. They are useful for application involving multiple members. Where values need not be assigned to all the members at any one time. +ike structures union can be declared using the keyword union as follows8 union item { int m; float p; char c; } code; this declares a variable code of type union item. The union contains three members each with a different data type. =owever we can use only one of them at a time. This is because if only one location is allocated for union variable irrespective of si,e. The compiler allocates a piece of storage that is large enough to access a union member we can use the same synta# that we use to access structure members. That is code%m code%p code%c are all valid member variables. >uring accessing we should make sure that we are accessing the member whose value is currently stored. "or e#ample a statement such as code%m/?!@; code%p/?!@%<0; printf)*+d,"code%m-; Would prodece erroneous result. In effect a union creates a storage location that can be used by one of its members at a time. When a different number is assigned a new value the new value supercedes the previous members value. 7nions may be used in all places where a structure is allowed. The notation for accessing a union member that is nested inside a structure remains the same as for the nested structure. "ead #ext: C Programming - Pointers Example: main)- { $oid starline)-; $oid message)-; AAAAAAA } $oid printline { statements; } $oid $alue { statements; }