Page 1 :
Unit No. 3 - Structures and Union, Definition:, A structure is a user defined data type used to collect members of different, data types. The name given to the structure is called a „Structure tag.‟., The general format of a structure definition is as follows:, Struct tag _name, {, datatype member1;, datatype member2;, ., ., ., datatypememberN;, }, Where,, 1) Struct is the keyword., 2) tag-name is the name of structure., 3) Member1,Member2,.....MemberN are the members of structure of specified data, types., 4) Structure template is terminated with semicolon., Declaring structure variables:, After defining a structure template, we can declare variables of structure type., Consider the following example., Struct student, {, int rollno;, char name[20];, float marks;, }s1,s2,s3;, Here before completing structure template s1,s2,s3 variables are created of type struct, student.
Page 2 :
Another method for creating structure variables are:, Struct student, {, int rollno;, char name[20];, float marks;, };, Struct student s1,s2,s3;, Remember that the members of a structure themselves are not variables. They do not, occupy any memory until variable of structure type is created., Structure Initialization:, Like any other variables, a structure variable can be initialized at, compile time., Struct student, {, int rollno;, char name[20];, float marks;, } s1={101,”Ram”,70.15};, Another method for initializing structure variable is, Struct student, {, int rollno;, char name[20];, float marks;, };, Struct student s1={101,”Ram”,50.55};, Struct student s2={102,”Sham”,60.00};, Rules for initializing Structures:, 1) We cannot initialize individual members inside the structure template because, memory is not allocated to member until variable of structure type is created., 2) The order of values enclosed in braces must match the order of members in the, structure definition., , 2
Page 3 :
3) It is permitted to have a partial initialization. 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., 4) The uninitialized members will be assigned default values as follows., zero for integer and floating point numbers., ’\0’ for characters and strings., Accessing structure members:, We can access and assign values to the members of a structure in, a number of ways. As mentioned earlier, the members themselves are not variables., The link between a member and a variable is established using the member operator, „.‟ which is also known as „dot operator’ or ‘period operator’., For example, s1.rollno, Where s1 is structure type variable and rollno is the member of structure., Program to demonstrate initialization and accessing structure members., #include<stdio.h>, #include<conio.h>, struct student, {, int rollno;, char name[20];, float marks;, };, void main(), {, struct student s1={101,”Ram”,55.35};, struct student s2;, printf(“\n input values”);, scanf(“%d%s%f”,s2.rollno,s2.name,s2.marks);, printf(“\n s1=%d\t%s\t%f”,s1.rollno,s1.name,s1.marks);, printf(“\n s2=%d\t%s\t%f”,s2.rollno,s2.name,s2.marks);, getch();, }, Output:, Input values, 102, , “Sham”, , 70.17, , s1=101, , “Ram”, , 55.35, , s2=102, , “Sham”, , 70.17, 3
Page 4 :
Passing structure to function:, Like a normal variable, a structure variable can also be passed to a, function as parameter. There are two methods of passing structure variable to a function, as argument., 1) Passing individual structure element as a argument, 2) Passing entire structure as a argument, 1) Passing individual structure element:, Consider following example., #include<stdio.h>, #include<conio.h>, Struct student, {, int rollno;, char name[20];, float marks;, };, Void main(), {, struct student s1;, void display(int,char*,float); /*function prototyping*/, printf(“\n enter students roll no,name and marks”);, scanf(“%d %s %f”,&s1.rollno,s1.name,&s1.marks);, display(s1.rollno,s1.name,s1.marks);, getch();, }, void display(intrno, char *n, float m), {, printf(“\n %d\t %s\t %f”,rno,n,m);, }, Output:, Enter students Rollno, name and marks 101 “Ram” 55.55, 101 Ram 55.55, 2) Passing entire structure at a time:, Consider following example., 4
Page 5 :
#include<stdio.h>, #include<conio.h>, struct student, {, int rollno;, char name[20];, float marks;, };, Void main(), {, struct student s1;, void display(struct student);, printf(“\n Enter students Rollno,name& marks”);, scanf(“%d %s %f”,&s1.rollno,s1.name,&s1.marks);, display(s1);, getch();, }, Void display(Struct student S, {, printf(“\n Rollno=%d”,s.rollno);, printf(“\n Name=%s”,s.name);, printf(“\n Marks=%f”,s.marks);, }, Output:, Enter students Rollno, name & marks, 101 Ram 55.55, Rollno= 101, Name= Ram, Marks= 55.55, Array of structures:, As we can create array of integers (int a[10];), array of floats (float, b[50];), array of characters (char c[20];) like same way we can also create array of, structure variables (struct student s[50];) known as array of structures., Consider the example that stores the record of a class of 50 students., #include<stdio.h>, #include<conio.h>, struct student, 5
Page 6 :
{, int rollno;, char name[20];, float marks;, };, void main(), {, struct student s[50]; /* array of structure*/, inti, clrscr();, for(i=0;i<50;i++), {, printf(“\n Enter roll no”);, scanf(“%d”,&s[i].rollno);, printf(“\n Enter name”);, scanf(“%s”,s[i].name);, printf(“\n Enter marks”);, scanf(“%f”,&s[i].marks);, }, printf(“\n Information of a class is as follows”);, for(i=0;i<50;i++), {, printf(“\n Roll no=%d”,s[i].rollno);, printf(“\n Name=%s”,s[i].name);, printf(“\n Marks=%f”,s[i].marks);, }, getch();, }, Array within structure:, When we create an array of structure members then it is known as, array within structure., For example, struct student, {, int rollno;, char name[20]; /* Array within structure*/, float marks;, };, 6
Page 7 :
Pointer to structure:, When pointer points to integer variable then it is known as pointer to, integer, when it points to floating point variable it is known as pointer to float. Like the, same way when pointer points to structure type variable it is known as pointer to, structure., Consider following example., struct student, {, int rollno;, char name[20];, float marks;, }s1;, struct student *ptr; /* pointer to structure*/, ptr=&s1;, , ptr, 1000, , 5000, , s1.name, , s1.name, , s.marks, , 1002, , 1022, , 1000, , When we use pointer to structure members of structure can be accessed different ways., 1) Using the pointer and indirection operator, (*ptr).rollno,(*ptr).name,(*ptr).marks, 2) Using the pointer and the membership operator., Ptrrollno, ptr name, ptr marks, , Nested structure:, Structure within a structure is called nested structure. Consider the, following example to store marks of three subject of students as., Struct student, {, int rollno;, char name[20];, int sub1;, int sub2;, 7
Page 8 :
int sub3;, }s1;, The above structure can be defined as, Struct student, {, introllno;, char name[20];, struct, {, int sub1;, int sub2;, int sub3;, }marks;, }s1;, Here, student structure contains a member named marks which itself is a structure with, three members., The members of inner structure can be accessed as follows., s1.marks.sub1;, s1.marks.sub2;, s1.marks.sub3;, , Union :A union is a variable that contains multiple members of possibly different, data types grouped together under a single name. However, all of them share the same, memory location. Hence, only one of the member will be active at a time., Following is the general syntax of union definition., union union-name, {, Datatype member1;, Datatype member2;, :, :, DatatypememberN;, }var1,var2,...varN;, , 8
Page 9 :
For example, union sample, {, int a;, float b;, char c;, }s;, This declares a variable s of type union sample. The union contains three members,, each with a different data type. However, we can use only one of them at a time., Because only one memory location of largest member size is allocated., Storage of 4 bytes, , 1000, , 1001, , 1002, , 1003, , C, a, , b, fig. sharing of a storage location by union members., Accessing members of the union:, Members of the union can be accessed like same way as accessing, structure members., The general syntax is as follows., union-variable.member, for example, s.a;, s.b;, s.c;, Initialization of union:, Union may be initialized when the variable is declared. But unlike, structure, it can be initialized only with a value of the same type as the first union, member., , 9
Page 10 :
For example:, union sample s={100};, is valid but the declaration, union sample s={10.00};, is invalid. Because we cannot initialize other than first member. 10.00 is floating, point number which is second member is union., , Difference between structure and union:Although the syntax for declaring and accessing structure and union, variables is the same, there are important differences between them., 1) Memory allocation:, Each member of a structure is allocated separate memory space. i.e. a, structure variable occupies the sum of sizes of all members in the variables., In case of a union, the amount of memory required is the same as its largest member., For example., struct sample, , union sample, , {, , {, int a;, , int a;, , float b;, , float b;, , char c;, , char c;, , }s1;, , }s1;, , Total memory for s1=7 bytes, , Total memory s1=4 bytes, , 2) Accessing members:, All the structure members can be accessed at any given time., Only one member of the union can be accessed at any given time., , 3) Initialization:, All members of a structure variable can be initialized., Only first member of a union variable can be initialized., , 10
Page 11 :
What is Enum ?, An enumeration is a user-defined data type that consists of integral constants. To define an, enumeration, keyword enum is used., , enum flag { const1, const2, ..., constN };, , Here, name of the enumeration is flag., And, const1, const2,...., constN are values of type flag., By default, const1 is 0, const2 is 1 and so on. You can change default values of enum elements, during declaration (if necessary)., , // Changing default values of enum, enum suit {, club = 0,, diamonds = 10,, hearts = 20,, spades = 3,, };, , Enumerated Type Declaration, When you create an enumerated type, only blueprint for the variable is created. Here's how you can, create variables of enum type., , enum boolean { false, true };, enum boolean check;, , Here, a variable check of type enum boolean is created., Here is another way to declare same check variable using different syntax., , enum boolean, {, false, true, } check;, , 11
Page 12 :
Example: Enumeration Type, #include <stdio.h>, , enum week { sunday, monday, tuesday, wednesday, thursday, friday, saturday };, , int main(), {, enum week today;, today = wednesday;, printf("Day %d",today+1);, return 0;, }, , Output, , Day 4, , Typedef Keyword in C:, The C programming language provides a keyword called typedef, which you, can use to give a type a new name. Following is an example to define a, term BYTE for one-byte numbers −, typedef unsigned char BYTE;, , After this type definition, the identifier BYTE can be used as an abbreviation for, the type unsigned char, for example.., BYTE, , b1, b2;, , By convention, uppercase letters are used for these definitions to remind the, user that the type name is really a symbolic abbreviation, but you can use, lowercase, as follows −, typedef unsigned char byte;, , You can use typedef to give a name to your user defined data types as well., For example, you can use typedef with structure to define a new data type and, then use that data type to define structure variables directly as follows −, Live Demo, #include <stdio.h>, #include <string.h>, , typedef struct Books {, char title[50];, , 12
Page 13 :
char author[50];, char subject[100];, int book_id;, } Book;, , int main( ) {, , Book book;, , strcpy( book.title, "C Programming");, strcpy( book.author, "Nuha Ali");, strcpy( book.subject, "C Programming Tutorial");, book.book_id = 6495407;, , printf( "Book title : %s\n", book.title);, printf( "Book author : %s\n", book.author);, printf( "Book subject : %s\n", book.subject);, printf( "Book book_id : %d\n", book.book_id);, , return 0;, }, , When the above code is compiled and executed, it produces the following result, −, Book, Book, Book, Book, , title : C Programming, author : Nuha Ali, subject : C Programming Tutorial, book_id : 6495407, , 13