KEMBAR78
C Structure and Union | PDF | Information Technology | Software Development
0% found this document useful (0 votes)
6 views21 pages

C Structure and Union

Uploaded by

45yg2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views21 pages

C Structure and Union

Uploaded by

45yg2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Structure and Union

Structure
• A Structure is a collection of related data items, possibly of different types.

• A structure type in C is called struct.

• A struct is heterogeneous in that it can be composed of data of different types.

• In contrast, array is homogeneous since it can contain only data of the same type.
Structure
• Structures hold data that belong together.
• Examples:
• Student record: student id, name, major, gender, start year, …
• Bank account: account number, name, currency, balance, …
• Address book: name, address, telephone number, …
• In database applications, structures are called records.
Structure
• Individual components of a struct type are called members (or fields).

• Members can be of different types (simple, array or struct).

• A struct is named as a whole while individual members are named using field identifiers.

• Complex data structures can be formed by defining arrays of structs.


Defining a Structure
struct [structure tag] Example
{ struct Books
member definition; {
member definition; char title[50];
. char author[50];
. char subject[100];
. int book_id;
member definition; } book;
} [one or more structure variables];
Defining a Structure
struct StudentRecord{
char Name[15];
Name
int Id;
char Dept[5]; Id Gender
char Gender; Dept
}student1;
Student1
Accessing Structure Members
The members of a struct type variable are accessed with the dot (.) operator:

Example
.
Structure_variable_name member_name; book.title;
book.author;
book.subject;
book.book_id;
Accessing Structure Members
Example
Name
strcpy(Student1.Name, "Chan Tai Man");
Student1.Id = 12345; Student1
Id Gender
strcpy(Student1.Dept, "COMP");
Student1.gender = 'M’; Dept

Chan Tai Man

12345 M

COMP
//Structure example
#include<stdio.h>
#include<conio.h>
struct Books{
char title[50]; Output
char author[50];
char subject[100]; Enter Book Title: programming
int book_id; Enter Author name: Dennis
}book;
Enter subject name: c
void main(){
printf("\n Enter Book Title:\t"); Enter book id: 1
scanf("%s", book.title);
printf("\nEnter Author name:\t"); Displaying Employee record:
scanf("%s", book.author);
printf("\nEnter subject name:\t");
scanf("%s", book.subject); Book title : programming
printf("\nEnter book id:\t"); Book author : Dennis
scanf("%d", &book.book_id);
Book subject : c
printf("\nDisplaying Employee record:\n");
printf("\nBook title : %s\n", book.title); Book book_id : 1
printf( "Book author : %s\n", book.author);
printf( "Book subject : %s\n", book.subject);
printf( "Book book_id : %d\n", book.book_id);
getch();
}
Nested structure
When a structure contains another structure, it is called nested structure.

Example
struct structure1
{
- - - - - - - - --
----------
};
struct structure2
{
- ---------
----------
struct structure1 obj;
};
void main()
#include<stdio.h> { Output :
struct Address int i;
Enter Employee Id : 101
struct Employee E;
{ Enter Employee Name : Suresh
printf("\n\tEnter Employee Id : ");
char HouseNo[25]; scanf("%d",&E.Id); Enter Employee Salary : 45000
char City[25]; printf("\n\tEnter Employee Name : "); Enter Employee House No : 4598/D
char PinCode[25]; scanf("%s",&E.Name); Enter Employee City : Delhi
}; printf("\n\tEnter Employee Salary : "); Enter Employee Pin Code : 110056
struct Employee scanf("%f",&E.Salary);
{ printf("\n\tEnter Employee House No : "); Details of Employees
int Id; scanf("%s",&E.Add.HouseNo);
Employee Id : 101
printf("\n\tEnter Employee City : ");
char Name[25]; Employee Name : Suresh
scanf("%s",&E.Add.City);
float Salary; printf("\n\tEnter Employee House No : "); Employee Salary : 45000
struct Address Add; scanf("%s",&E.Add.PinCode); Employee House No : 4598/D
}; printf("\nDetails of Employees"); Employee City : Delhi
printf("\n\tEmployee Id : %d",E.Id); Employee Pin Code : 110056
printf("\n\tEmployee Name : %s",E.Name);
printf("\n\tEmployee Salary : %f",E.Salary);
printf("\n\tEmployee House No : %s",E.Add.HouseNo);
printf("\n\tEmployee City : %s",E.Add.City);
printf("\n\tEmployee House No : %s",E.Add.PinCode);
}
Array of structure
• An ordinary array: One type of data

0 1 2 … 98 99

• An array of structs: Multiple types of data in each array element.

0 1 2 … 98 99
Array of structure
Example:

struct StudentRecord{

char Name[15]; Chan Tai Man


int Id;

char Dept[5]; 12345 M


char Gender;
COMP
};

StudentRecord Class[100];
strcpy(Class[98].Name, "Chan Tai Man");
Class[98].Id = 12345;
...
strcpy(Class[98].Dept, "COMP");
Class[98].gender = 'M';
Class[0] = Class[98];
0 1 2 … 98 99
Structure in functions
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[20];
float percentage; Output:
};
void func(struct student record);
int main() Id is: 1
{
struct student record; Name is: Raju
record.id=1;
strcpy(record.name, "Raju"); Percentage is: 86.500000
record.percentage = 86.5;

func(record);
return 0;
}
void func(struct student record)
{
printf(" Id is: %d \n", record.id);
printf(" Name is: %s \n", record.name);
printf(" Percentage is: %f \n", record.percentage);
}
Pointers and structure
#include <stdio.h>
struct person
{
int age;
Output:
float weight; Enter age: 21
}; Enter weight: 50
int main()
{
struct person *personPtr, person1; Displaying:
personPtr = &person1; Age: 21
printf("Enter age: "); weight: 50.000000
scanf("%d", &personPtr->age);
printf("Enter weight: ");
scanf("%f", &personPtr->weight);
printf("Displaying:\n");
printf("Age: %d\n", personPtr->age);
printf("weight: %f", personPtr->weight);
return 0;
}
Union
• Memory that contains a variety of objects over time
• Only contains one data member at a time
• Members of a union share space
• Conserves storage
• Only the last data member defined can be accessed

union declarations
• Same as struct, for example
union Number {
int x;
float y;
};
union Number union_variable_name;
Union
Choices: union AnElt {
int i;
An element is
char c;
• an int i or
} elt2;
• a char c
elt2.c = ’a’;
elt2.i = 0xDEADBEEF;
sizeof(union …) =maximum of sizeof(field)

c padding

EF BE AD DE
i
Union
Only one union member can be accessed at a time
Example:
#include <stdio.h>
union Job
{ Output:
float salary;
int workerNo; Salary = 0.0
} j; Number of workers = 100
int main()
{
j.salary = 12.3;
j.workerNo = 100;
printf("Salary = %.1f\n", j.salary);
printf("Number of workers = %d", j.workerNo);
return 0;
}
Union
Only one union member can be accessed at a time
Example:
#include <stdio.h>
union Job
{ Output:
float salary;
int workerNo; Salary = 12.3
} j; Number of workers = 0
int main()
{
j.workerNo = 100;
j.salary = 12.3;
printf("Salary = %.1f\n", j.salary);
printf("Number of workers = %d", j.workerNo);
return 0;
}
Similarity between Structure and Union
• Both structure and union have the same way of declaring itself, creating variables and the same
way of accessing members of the variables.
• Both Structure and union can be passed to a function by both the methods call by value and
call by reference.
• Both Structure and union are container data type & contain an object of any data type,
including other structure, union, array as their members.
Difference between Structure and Union
BASIS OF COMPARISON STRUCTURE UNION
Basic The separate memory location is allotted to All members of the 'union' share the same
each member of the 'structure'. memory location.

Declaration struct struct_name{ union u_name{


type element1; type element1;
type element2; type element2;
. .
. .
} variable1, variable2, ...; } variable1, variable2, ...;
keyword 'struct' 'union'
Size Size of Structure= sum of size of all the data Size of Union=size of the largest members.
members.
Store Value Stores distinct values for all the members. Stores same value for all the members.
At a Time A 'structure' stores multiple values, of the A 'union' stores a single value at a time for all
different members, of the 'structure'. members.
Way of Viewing Provide single way to view each memory Provide multiple way to to view same memory
location. location.

You might also like