Chapter 10 - Structures, Unions & Enumerations
Chapter 10 - Structures, Unions & Enumerations
◼ Enumerations
➢ All About Enumerations
➢ Programming Examples
For Example, suppose we are developing a program for some University and
we want to represent a student. A “student” can be thought of as a combination
of the following elements:
➢ Registration number (int)
➢ Name (string)
➢ CGPA (float)
◼ Syntax:
◼ NOTES:
1. When a structure is defined, only a new data type (user-defined data type) is
created, e.g., the above code creates a new user-defined data type
“struct student”. However, no memory is allocated to it (memory is allocated
to a variable, not to a data type).
2. The structure elements can be of any data type: primary (except void, i.e.,
int, char, float, double), derived (arrays, pointers), and even user-defined
(structure, unions, enumeration). (We shall see some examples later.)
Dr. N. K. Pani, Dept. of CSEA, IGIT Sarang | 10.7
3. Normally, a structure should be defined at the beginning of the program,
outside of all functions. This allows the structure definition to be global
and hance can be used by all the functions in the program (which we
often want).
However, if we exclusively want to make a structure local to a
particular function, then we can define it within that function.
#include <stdio.h>
struct Student /* Ideal place for defining a structure. */
{
…
};
void main()
{
…
}
◼ After a structure is defined, only a new data type (user-defined data type) is
created. So, in order to use it, we need to declare variables of its type.
◼ Syntax:
struct structure_name variable_name;
◼ NOTES:
1. Memory allocation is done at the time of structure variable declaration.
For Ex, the following code would result in the given memory allocation.
struct Student
{ regdNo name cgpa
int regdNo;
char name[30]; s
float cgpa;
}; Ox20 Ox22 Ox72
struct Student s;
#include <stdio.h>
struct Student /* Ideal place for defining a structure. */
{
…
};
void main()
{
struct Student s1, s2; /* Ideal place for declaring structure variables. */
…
}
Syntax 1:
struct Student
{
int regdNo;
char name[30];
float cgpa;
} s1, s2, s3; /* Three variables of type “struct student” are created */
The above code would mean that a new structure type is created and is
renamed to “Student”. Therefore, the following declaration can be used
to create structure variables.
struct Student
{
int regdNo;
char name[30];
float cgpa;
};
struct Student s;
s.redgNo;
s.name[];
s.cgpa;
/* PR10_1.c: Define a structure “Student” having regd. no., name, and CGPA as attributes. Write a
program to read the information for one student from the keyboard and print the same on the screen. */
# include <stdio.h> Output
# include <conio.h>
Enter the student’s Regd. No.,
struct Student /* Structure definition */ Name, and CGPA: 12345 Kartik 9.62
{
int regdNo; Student Details
char name[30]; ---------------
float cgpa; Regd. No.: 12345
}; Name: Kartik
void main() CGPA: 9.620000
{
struct Student s; /* Structure declaration */
printf("\nStudent Details\n---------------");
printf("\nRegd. No.: %d\nName: %s\nCGPA: %f\n",
s.regdNo, s.name, s.cgpa); /* Accessing members*/
getch();
}
struct Person
{
int id;
char name[30];
int age;
};
void main()
{
struct Person per1 = {2391, “Ram”, 25}; /* Declaration at initialization. */
struct Person per2;
...
per2 = {7895, “Kartik”, 46}; /* Declaration in a separate statement. */
...
}
For Example: If per1, per2, and per3 are variables of the same structure, then
the following statements are valid.
per1 = per2;
per2 = per3;
For Example: If per1, per2, and per3 are some structure variables (of the same /
different structures), the following statements are not permitted.
per1 == per2;
per2 != emp3;
[NOTE]: In case, we need to compare two structure variables of the same structure type, we
may do so by comparing individual members.
void main()
{
Person per1 = {12345, "Ravi", 32};
Person per2;
per2 = per1; /* Copying */
◼ Programming Example:
for (i=0;i<3;i++)
{
printf("\nStudent %d\n----------\n", i+1);
printf("Regd. No.: ");
scanf("%d", &studs[i].regdNo);
printf("Name: ");
scanf("%s", studs[i].name);
printf("CGPA: ");
scanf("%f", &studs[i].cgpa);
}
printf("\n\nStudents Details\n================\n");
for (i=0;i<3;i++)
{
printf("\nDetails of Student %d\n--------------------", i+1);
printf("\nRegd. No.: %d\nName: %s\nCGPA: %.2f\n",
studs[i].regdNo, studs[i].name, studs[i].cgpa);
}
getch();
}
struct Student
{
int regdNo;
char name[30];
float cgpa[SEMESTERS];
}; [Cont.]
printf("Name: ");
scanf("%s", studs[i].name);
getch();
}
Students Information
=====================
◼ Syntax 1:
struct Date
{
int day;
char month[10];
int year;
} joiningDate; /* A structure “struct Date” is defined, and its variable is declared
within the structure “struct Employee. */
float salary;
};
typedef struct
{
int day;
char month[10];
int year;
} Date;
struct Employee
{
char name[30];
Date joiningDate; /* Structure variable declared within another structure */
float salary;
};
[Cont.]
printf("Name: ");
scanf("%s", e.name);
printf("Salary: ");
scanf("%f", &e.salary);
printf("\n\nEmployee Details\n----------------");
printf("\nName: %s\nDate of Joining: %d %s %d\nSalary: %.2f ",
e.name,
e.joiningDate.day, e.joiningDate.month, e.joiningDate.year,
e.salary);
getch();
}
Employee Details
----------------
Name: Remesh
Date of joining is: 20 March 1999
Salary: 36000.00
struct Student
{
int regdNo;
char name[30];
float cgpa;
};
void main()
{
struct Student *s; /* s is a pointer to “struct Student”, i.e.,
it can store the address of a variable of type “struct Student”. */
...
}
For Example: For the structure definition and declaration given in the last slide,
its members could be accessed as:
(*s).redgNo; S->redgNo;
(*s).name[]; or S->name[];
(*s).cgpa; S->cgpa;
void main()
{
struct Employee e1, *e2; /* e1 is a normal variable of type “struct Employee”.
e2 is a pointer to “struct Employee”, i.e., it can store the
address of a variable of type “struct Employee”. */
e2 = &e1; /* A pointer variable must be assigned to an address of its type. */
[Cont.]
Dr. N. K. Pani, Dept. of CSEA, IGIT Sarang | 10.33
printf("\nEnter employee information\n");
printf("==========================\n\n");
printf("Name: ");
scanf("%s", e2->name); /* Access through pointer */
printf("Salary: ");
scanf("%f", &e2->salary); /* Access through pointer */
printf("\nEmployee Details\n----------------");
Employee Details
----------------
Name: Remesh
Date of joining is: 20 March 1999
Salary: 36000.00
Name: Remesh
Date of joining is: 20 March 1999
Salary: 36000.00
Name: Remesh
Date of joining is: 20 March 1999
Salary: 36000.00
◼ In terms of syntax, both structures and unions are almost the same (have minor
differences). However, there is a major difference between them in terms of
storage (memory allocation).
Unlike a structure which is defined and whose variables are declared with the
keyword struct, a union is defined, and its variables are declared with the
keyword union.
union Item
{
int code;
char name[10];
float price;
};
void main()
{
union Item b;
...
}
Unions can be useful where any one of the members of the variable is sufficient
to give complete information about the variable.
[Code 2]
Dr. N. K. Pani, Dept. of CSEA, IGIT Sarang | 10.42
◼ Difference 3 - Regarding Accessing the Members:
struct Item
{
int code;
char name[10];
float price;
};
void main()
{
struct Item a;
a.code = 12345;
a.name = “Tea”;
a.price = 30.5;
printf(“%d”, a.code); /* Prints 12345 */
printf(“%s”, a.name); /* Prints Tea */
printf(“%f”, a.price); /* Prints 30.500000 */
}
void main()
{
union Item b;
b.code = 12345;
b.name = “Tea”;
b.price = 30.5;
printf(“%d”, b.code); /* Prints erroneous value */
printf(“%s”, b.name); /* Prints erroneous value */
printf(“%f”, b.price); /* Prints 30.500000 */
}
While initializing a structure at the place of declaration, we can initialize all its
members. However, while initializing a union at the place of declaration, only
its first member can be initialized.
union StudentId
{
int rollNo;
int regdNo;
};
struct Student
{
union StudentId id;
char name[30];
float cgpa;
};
void main()
{
int i;
char choice;
getch();
}
Students Information
====================
Details of Student 1
--------------------
Roll No.: 31
Name: Remesh
CGPA: 8.60 [Cont.]
Details of Student 1
--------------------
Regd. No.: 35984
Name: Kartik
CGPA: 9.90
Details of Student 1
--------------------
Roll No.: 32
Name: Shiv
CGPA: 9.1
For Example:
enum Day
{
Sunday, /* Automatically assigned to 0. */
Monday = 3, /* Explicitly assigned to 3. */
Tuesday, /* Automatically assigned to 4. */
Wednesday, /* Automatically assigned to 5. */
Thursday, /* Automatically assigned to 6. */
Friday = 27, /* Explicitly assigned to 27. */
Saturday /* Automatically assigned to 28. */
};
enum Day{Sun, Mon, Tue, Wed, Thu, Fri, Sat} weekStart, weekEnd;
Dr. N. K. Pani, Dept. of CSEA, IGIT Sarang | 10.55
Programming Examples
◼ Programming Example 1:
# include <stdio.h>
void main()
{
enum Day today;
today = Wed;
printf("Today is day %d of the week.\n", today);
}
Output
# include <stdio.h>
enum month{Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct,
Nov, Dec};
void main()
{
enum month i;
for (i=Jan; i<=Dec; i++)
{
printf("%d ", i);
}
}
Output
0 1 2 3 4 5 6 7 8 9 10 11