Module 4 Structure and Union
Module 4 Structure and Union
A structure is a user-defined data type which groups logically related items under one single unit.
The memory occupied by structure variable is the sum of sizes of all the members
struct Empl
{
int emp_id;
float salary;
char designation[20];
int depart_no;
int age_of_emp;
};
Declaring structure variable
struct employee
{ int id;
char name[50];
float salary;
};
main()
{
struct employee e1, e2;
The variables e1 and e2 can be used to access the values stored in the structure.
struct employee
{ int id;
char name[50];
float salary;
}e1,e2;
#include<stdio.h>
#include <string.h>
struct employee
{ int id;
char name[50];
}e1; //declaring e1 variable for structure
int main( )
{
//store first employee information
e1.id=101;
strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array
//printing first employee information
printf( "employee 1 id : %d\n", e1.id);
printf( "employee 1 name : %s\n", e1.name);
return 0;
}
#include<stdio.h> //store second employee information
char name[50];
}e1,e2; //declaring e1 and e2 variables for structure printf( "employee 1 id : %d\n", e1.id);
return 0;
}
#include <stdio.h>
struct stud
{
int roll;
char name[50];
float marks;
} s;
void main()
{
printf("Enter the name of student: ");
scanf("%s", s.name);
printf("Enter his/her roll number: ");
scanf("%d", &s.roll);
printf("Enter his/her marks: ");
scanf("%f", &s.marks);
printf("Displaying the Information:\n");
printf("Name: ");
puts(s.name);
printf("Roll number: %d\n",s.roll);
printf("Marks: %f\n", s.marks);
printf("\nEnter detailsof the Employee %d",i+1);
#include<stdio.h> printf("\n\tEnter Employee Id : ");
struct Employee
scanf("%d",&E[i].Id);
{
int Id; printf("\n\tEnter Employee Name : ");
int Age; scanf("%s",&E[i].Name);
char Name[25];
printf("\n\tEnter Employee Age : ");
long Salary;
}; scanf("%d",&E[i].Age);
void main() printf("\n\tEnter Employee Salary : ");
{
scanf("%ld",&E[i].Salary);
int i;
struct Employee E[ 3 ]; }
for(i=0;i<3;i++) printf("\nDetails of Eloyees");
{
for(i=0;i<3;i++)
{
printf("\n%d\t%s\t%d\t%ld",E[i].Id,E[i].Name,E[i].Age,E[i].Salary);
} }
Structures within a structure:
1. struct struct_name;
2. struct_name
The syntax to access the structure within another structure is:
3. {
4. data_type variable_name;
struct _variable. nested_struct_variable. struct_member;
5. struct struct_name
6. {
7. data_type variable_name;
8. ........
9. }struct_variable;
10. } variable_name;
1. struct employee
2. {
3. int emp_id;
e1.doj.day; -> for day in date
4. char name[20];
5. float salary; e1.doj.month; -> for month in date
6. int dept_no; e1.doj.year; -> for year in month
7. struct date
8. {
9. int day;
10. int month;
11. int year;
12. }doj;
13. }e1;
#include <stdio.h>
struct Emp
{
int id;
struct Date
{
int dd;
int mm;
int yyyy;
}doj;
}e1;
void main( )
{
e1.id=101;
e1.doj.dd=10;
e1.doj.mm=11;
e1.doj.yyyy=2014;
printf( "employee id : %d\n", e1.id);
printf( "employee date of joining (dd-mm-yyyy) : %d/%d/%d\n", e1.doj.dd,e1.doj.mm,e1.doj.yyyy);
}
1. #include<stdio.h>
2. struct address
3. {
4. char city[20];
5. int pin;
6. char phone[14];
7. };
8. struct employee
9. {
10. char name[20];
11. struct address add;
12. }emp;
13. void main ()
14. {
15. struct employee emp;
16. printf("Enter employee information?\n");
17. scanf("%s %s %d %s",emp.name,emp.add.city, &emp.add.pin, emp.add.phone);
18. printf("Printing the employee information....\n");
19. printf("name: %s\nCity: %s\nPincode: %d\nPhone: %s",emp.name,emp.add.city,emp.add.pin,emp.add.phone);
20. }
Operator in structure
operator is a member of the highest precedence group, this operator will take
precedence over the unary operators as well as the various arithmetic, relational,
logical and assignment operators.
if we wanted to access the account number for the 14th customer , write as
customer[ 13] .acct_no.
In this declaration age is a user-defined data type, which is equivalent to type int.
is equivalent to writing
type *ptvar;
where type is a data type that identifies the composition of the structure, and ptvar
represents the name of the pointer variable
ptvar = &variable;
typedef struct
{
int acct-no;
customer is a structure variable of type account,
char acct-type;
and pc is a pointer variable whose object is a
char name[bO]; structure variable of type account.
the beginning address of customer can be assigned
float balance;
to pc by writing
} account; pc = &customer;
account customer, *pc;
pointer variable pc is initialized by
assigning it the beginning address of the
structure variable customer. pc will
point to customer.
Customer.acct-no
pc->acct-no
(*pc).acct-no
customer's balance can be accessed by writing any of the following:
customer.balance pc->balance (*pc).balance
the customer's name can be accessed by writing any of the following: customer.
name pc ->name (*pc).name
#include <stdio.h>
int main( )
{
int n = 3333;
char t = 'C';
float b = 99.99;
typedef struct {
int month;
int day;
int year;
} date;
struct {
int *acct_no;
char *acct_type;
char *name;
float *balance;
date lastpayment;
} customer, *pc = &customer;
customer.acct_no = &n;
customer.acct_type = &t;
customer.name ="Smith";
customer.balance = &b;
printf("%d %c %s %.2f\n", *customer.acct_no, *customer.acct_type,customer.name, *customer.balance);
printf("%d %c %s %.2f\n", *pc->acct_no, *pc->acct_type, pc->name, *pc->balance);
}
PASSING STRUCTURES TO FUNCTIONS
To pass structure-type information to or from a function,
Within ad just, the value assigned to newbalance presumably makes use of the
information passed to the function.
This value is then returned to main, where it is assigned to the structure member
customer. balance.
SELF-REFERENTIAL STRUCTURES
within a structure one member that is a pointer to the parent structure type.
A union is declared and used in the same ways that a structure. Unions are defined
and declared in the same fashion as structures.
In unions, all the members share the space which is according to the space
requirement of the largest member.
A union can be initialized on its declaration. Because only one member can be used
at a time, only one can be initialized.
To avoid confusion, only the first member of the union can be initialized.
Union Data Type
The union data type allocates the space equal to space needed to hold the largest
data member of union.
The union allows different types of variable to share same space in memory.
There is no other difference between structure and union than internal difference.
The method to declare, use and access the union is same as structure.
#include <stdio.h>
#include <string.h>
union Data {
int i;
float f;
char str[20];
};
int main( ) {
return 0;
}
#include <stdio.h>
#include <string.h>
union Data {
int i;
float f;
char str[20];
};
int main( ) {
union Data data;
data.i = 10;
data.f = 220.5;
strcpy( data.str, "C Programming");
printf( "data.i : %d\n", data.i);
printf( "data.f : %f\n", data.f);
printf( "data.str : %s\n", data.str);
return 0;
}
#include<stdio.h>
union job
{ char name[32];
float salary;
int worker_no;
} u;
struct job1
{ char name[32];
float salary;
int worker_no;
} s;
void main( )
{ printf("size of union = %d", sizeof(u));
printf("\nsize of structure = %d", sizeof(s));
}