User-Defined Data Types
(struct-enum-union)
Week 08
CC213:Programming Applications
Agenda
 Structures
 What is a structure?
 Using structures
 Structure and Functions
 Structure and Pointers
 Structure and Arrays
 Nested Structures
 Enumeration
 Union
Structure is the collection of variables of different types
under a single name for better handling.
Examples:
• you may want to store information about a student:
id & gpa
• you may also want to store information about a person:
name, citizenship number and salary.
 Keyword struct is used for defining a structure.
What is a Structure?
1. Define the structure.
2. Declare/Initialize instances/variables of the structure.
3. Access members of an instance of the structure.
Using Structures
Syntax:
struct structure_name
{ data_type member1;
data_type member2;
.
.
data_type memeber;
};
1.Defining a Structure
Example:
struct Student
{ int id;
float gpa;
};
Name of data type
 When a structure is defined, it creates a user-
defined data type but, no storage is allocated.
 Example: variables can be declared from Person
structure as following:
2.Declaring Structure Variables
struct Person
{ char name[50];
int cit_no;
float salary;
};
Inside/outside main function:
struct Person p1, p2, p[20];
struct Person
{ char name[50];
int cit_no;
float salary;
}p1 ,p2 ,p[20];
Single
Variables
declaration
Array of
Variables declaration
Name of data type
 There are two types of operators used for
accessing members of a structure:
3.Accessing Members of a
Structure Variable
• Syntax: structure_variable_name.member_name
• Example, to access salary member via variable p2:
p2.salary=100;
2.Structure pointer operator( )
1.Member operator(.)
• Syntax: structure_pointer_name->member_name
• Example, to access salary member via pointer ptr:
Person * ptr; ptr=&p2;
ptrsalary=200;
1. Manipulating individual component
 Example: strcpy(current_planet.name, "Jupiter");
current_planet.diameter = 142980;
current_planet.moons = 16;
current_planet.orbit_time = 11.9;
current_planet.rotation_time = 9.925;
2. Manipulating whole structure
 Example: previous_planet = current_planet;
Extra Examples: Manipulating
a structure
 Programmer generally use typedef while defining
structures in C language. For example:
Defining a Structure using the
Keyword typedef
Syntax:
typedef struct
{ data_type member1;
data_type member2;
.
.
data_type memeber;
} structure_name;

Example1:
typedef struct
{ int imag;
float real; } Complex;
Inside/Outside main function:
Complex c1,c2;
 Here, typedef keyword is used for creating a type
Complex (which is of type as struct Complex). Then,
two structure variables c1 and c2 are created by
this Complex type.
Name of data type
Example 2:
typedef struct{
char name[10];
double diameter;
int moons;
double orbit_time, rotation_time;
}Planet_t;
int main (void)
{
int x;
Planet_t current_planet;
Planet_t previous_planet;
planet_t blank_planet = { " ", 0, 0, 0, 0};
float y;
return 0;
}

1.
2.
In C, structure can be passed as input parameter
To functions by two methods:
Passing by value (passing actual value as argument)
Passing by reference (passing address of an argument)
Structure and Functions
#include<stdio.h>
struct Student{
char name[50];
int roll;
};
void Display (struct Student stu);
int main()
{struct Student s1;
printf("Enter student's name:");
scanf("%s",s1.name);
printf("Enter roll number:");
scanf("%d",&s1.roll);
Display(s1);
return0;
}
void Display(struct Student stu)
{ printf("OutputnName:%s",stu.name);
printf("nRoll:%d",stu.roll);}
Output
Enter student's name: Kevin
Enter roll number: 149
Output
Name: Kevin
Roll: 149
Example1:
Passing structure by value
void print_planet(Planet_t *pa)
{
printf("%sn", pa->name); //you need to use ->
printf("the diameter is: %f kmn", pa->diameter);
printf("number of moons : %d n", pa->moons);
printf("orbit time is : %f yearsn", (*pa).orbit_time); //you can use *
also
}
void main()
{
planet_t current_planet = { "pluto", 15, 7, 13, 10};
print_planet(&current_planet);
}
Example2:
Passing structure by reference
• The following function receive structure as a reference
parameter to print out its contents:
Planet_t get_planet(void)
{
planet_t planet;
planet.name,
&planet.diameter,
&planet.moons,
&planet.orbit_time,
&planet.rotation_time);
}
void main()
{
scanf("%s%lf%d%lf%lf",
return(planet);
Planet_t current_planet;
current_planet = get_planet();
}
Example3: A function that
returns a structure
 Set of named integer constants that specifies all the
legal values that a variable of its type can have.
-Example:
enum Color {red, white, blue}
//or alternatively:
//typedef enum {red, white, blue} Color;
//in main function: define and initialize an enum variable:
Color c;
c = red;
c = white;
Enumeration
 The key point to understand about an
enumeration that each of the symbols stands
for an integer value and can be used in any
integer expression.
Enumeration
#include <stdio.h>
int main()
{
enum Days{Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday};
Days theDay;
int j = 0;
printf("Please day of the week (0 to 6)n"); scanf("%d",&j);
theDay = Days(j);
if(theDay == Sunday || theDay == Saturday)
printf("Hurray it is the weekendn");
else
printf("still at work ");
return 0;
}
Enumeration
Union
union Student {
int id;
float gpa;
char name[20];
};
int main( ) {
union Student x;
printf( "Memory size occupied by x union variable: %dn", sizeof(x));
return 0;
}
//output:
Memory size occupied by data :
20
• Union is similar to structure in definition. However, the
memory allocated for a union variable is shared between
union members and the size allocated is equal to the largest
member size.
• Example1:
Example2:
#include <string.h>
union Student {
int id;
float gpa;
char name[20];
};
int main( ) {
union Student x;
x.id = 10; //will use the 20 bytes
x.gpa = 3.5; //will use the same 20 bytes
strcpy( x.name, “Hassan Ahmed"); //will use the same 20 bytes
printf( “x.id : %dn", x.id);
printf( “x.gpa: %fn", x.gpa);
printf( “x.name : %sn", x.name);
return 0;
}
//output:
x.id : 1917853763
x.gpa :
4122360580327794860452759994368.000000
x.name : Hassan Ahmed
Example3:
#include <stdio.h>
#include <string.h>
union Student {
int id;
float gpa;
char name[20];
};
int main( ) {
union Student x;
x.id = 10;
printf( “x.id : %dn", x.id);
x.gpa = 3.5;
printf( “x.gpa : %fn", x.gpa);
strcpy( x.name, “Hassan Ahmed");
printf( “x.name : %sn", x.name);
return 0;
}
//output
x.id : 10
x.gpa : 3.500000
x.name : Hassan Ahmed
The End

More Related Content

PDF
structure1.pdf
PPTX
ECE2102-Week13 - 14-Strhhhhhhhjjjucts.pptx
PPT
C Language_PPS_3110003_unit 8ClassPPT.ppt
PDF
VIT351 Software Development VI Unit4
PPTX
Structures
PDF
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
PPTX
data structure and c programing concepts
PDF
Principals of Programming in CModule -5.pdf
structure1.pdf
ECE2102-Week13 - 14-Strhhhhhhhjjjucts.pptx
C Language_PPS_3110003_unit 8ClassPPT.ppt
VIT351 Software Development VI Unit4
Structures
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
data structure and c programing concepts
Principals of Programming in CModule -5.pdf

Similar to CC213-Week08-User-Defined Data Types.pptx (20)

PDF
Data Structure & Algorithm - Self Referential
PPT
Structures
PDF
Chapter15 structure
PPT
Introduction-to-structures-using-C-programming.ppt
PPTX
CHAPTER -4-class and structure.pptx
PPTX
PPTX
Programming for problem solving-II(UNIT-2).pptx
PPTX
Structures and Unions
PPTX
DS_Lecture_05.1_05.2.pptxnjnshsgxdhdgcdhcbdhc
PDF
Lk module4 structures
PPT
Structures and Unions in C-Language with Examples.ppt
PPTX
User Defined Datatypes in C++ (Union, enum, class)
PDF
PROBLEM SOLVING USING C PPSC- UNIT -5.pdf
PPT
C Programming Intro.ppt
PPT
Unit4 (2)
PPTX
Lecture_03.2(DatawwStructure) 7 (1).pptx
PPT
structures_v1.ppt
PPT
structures_v1.ppt
PPTX
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
PDF
slideset 7 structure and union (1).pdf
Data Structure & Algorithm - Self Referential
Structures
Chapter15 structure
Introduction-to-structures-using-C-programming.ppt
CHAPTER -4-class and structure.pptx
Programming for problem solving-II(UNIT-2).pptx
Structures and Unions
DS_Lecture_05.1_05.2.pptxnjnshsgxdhdgcdhcbdhc
Lk module4 structures
Structures and Unions in C-Language with Examples.ppt
User Defined Datatypes in C++ (Union, enum, class)
PROBLEM SOLVING USING C PPSC- UNIT -5.pdf
C Programming Intro.ppt
Unit4 (2)
Lecture_03.2(DatawwStructure) 7 (1).pptx
structures_v1.ppt
structures_v1.ppt
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
slideset 7 structure and union (1).pdf
Ad

Recently uploaded (20)

PDF
Chalkpiece Annual Report from 2019 To 2025
PPTX
Introduction to Building Information Modeling
PPT
robotS AND ROBOTICSOF HUMANS AND MACHINES
PPTX
8086.pptx microprocessor and microcontroller
PDF
GSH-Vicky1-Complete-Plans on Housing.pdf
PDF
Test slideshare presentation for blog post
PDF
trenching-standard-drawings procedure rev
PPTX
Bitcoin predictor project presentation
PPTX
UNIT III - GRAPHICS AND AUDIO FOR MOBILE
PPTX
Drawing as Communication for interior design
PDF
Trends That Shape Graphic Design Services
PDF
Govind singh Corporate office interior Portfolio
PPTX
Evolution_of_Computing_Presentation (1).pptx
PDF
How Animation is Used by Sports Teams and Leagues
PDF
Architecture Design Portfolio- VICTOR OKUTU
PDF
Designing Through Complexity - Four Perspectives.pdf
PDF
2025_AIFG_Akane_Kikuchi_Empathy_Design.PDF
PPT
aksharma-dfs.pptgfgfgdfgdgdfgdfgdgdrgdgdgdgdgdgadgdgd
PDF
321 LIBRARY DESIGN.pdf43354445t6556t5656
PDF
IARG - ICTC ANALOG RESEARCH GROUP - GROUP 1 - CHAPTER 2.pdf
Chalkpiece Annual Report from 2019 To 2025
Introduction to Building Information Modeling
robotS AND ROBOTICSOF HUMANS AND MACHINES
8086.pptx microprocessor and microcontroller
GSH-Vicky1-Complete-Plans on Housing.pdf
Test slideshare presentation for blog post
trenching-standard-drawings procedure rev
Bitcoin predictor project presentation
UNIT III - GRAPHICS AND AUDIO FOR MOBILE
Drawing as Communication for interior design
Trends That Shape Graphic Design Services
Govind singh Corporate office interior Portfolio
Evolution_of_Computing_Presentation (1).pptx
How Animation is Used by Sports Teams and Leagues
Architecture Design Portfolio- VICTOR OKUTU
Designing Through Complexity - Four Perspectives.pdf
2025_AIFG_Akane_Kikuchi_Empathy_Design.PDF
aksharma-dfs.pptgfgfgdfgdgdfgdfgdgdrgdgdgdgdgdgadgdgd
321 LIBRARY DESIGN.pdf43354445t6556t5656
IARG - ICTC ANALOG RESEARCH GROUP - GROUP 1 - CHAPTER 2.pdf
Ad

CC213-Week08-User-Defined Data Types.pptx

  • 1. User-Defined Data Types (struct-enum-union) Week 08 CC213:Programming Applications
  • 2. Agenda  Structures  What is a structure?  Using structures  Structure and Functions  Structure and Pointers  Structure and Arrays  Nested Structures  Enumeration  Union
  • 3. Structure is the collection of variables of different types under a single name for better handling. Examples: • you may want to store information about a student: id & gpa • you may also want to store information about a person: name, citizenship number and salary.  Keyword struct is used for defining a structure. What is a Structure?
  • 4. 1. Define the structure. 2. Declare/Initialize instances/variables of the structure. 3. Access members of an instance of the structure. Using Structures
  • 5. Syntax: struct structure_name { data_type member1; data_type member2; . . data_type memeber; }; 1.Defining a Structure Example: struct Student { int id; float gpa; }; Name of data type
  • 6.  When a structure is defined, it creates a user- defined data type but, no storage is allocated.  Example: variables can be declared from Person structure as following: 2.Declaring Structure Variables struct Person { char name[50]; int cit_no; float salary; }; Inside/outside main function: struct Person p1, p2, p[20]; struct Person { char name[50]; int cit_no; float salary; }p1 ,p2 ,p[20]; Single Variables declaration Array of Variables declaration Name of data type
  • 7.  There are two types of operators used for accessing members of a structure: 3.Accessing Members of a Structure Variable • Syntax: structure_variable_name.member_name • Example, to access salary member via variable p2: p2.salary=100; 2.Structure pointer operator( ) 1.Member operator(.) • Syntax: structure_pointer_name->member_name • Example, to access salary member via pointer ptr: Person * ptr; ptr=&p2; ptrsalary=200;
  • 8. 1. Manipulating individual component  Example: strcpy(current_planet.name, "Jupiter"); current_planet.diameter = 142980; current_planet.moons = 16; current_planet.orbit_time = 11.9; current_planet.rotation_time = 9.925; 2. Manipulating whole structure  Example: previous_planet = current_planet; Extra Examples: Manipulating a structure
  • 9.  Programmer generally use typedef while defining structures in C language. For example: Defining a Structure using the Keyword typedef Syntax: typedef struct { data_type member1; data_type member2; . . data_type memeber; } structure_name;
  • 10.  Example1: typedef struct { int imag; float real; } Complex; Inside/Outside main function: Complex c1,c2;  Here, typedef keyword is used for creating a type Complex (which is of type as struct Complex). Then, two structure variables c1 and c2 are created by this Complex type. Name of data type
  • 11. Example 2: typedef struct{ char name[10]; double diameter; int moons; double orbit_time, rotation_time; }Planet_t; int main (void) { int x; Planet_t current_planet; Planet_t previous_planet; planet_t blank_planet = { " ", 0, 0, 0, 0}; float y; return 0; }
  • 12.  1. 2. In C, structure can be passed as input parameter To functions by two methods: Passing by value (passing actual value as argument) Passing by reference (passing address of an argument) Structure and Functions
  • 13. #include<stdio.h> struct Student{ char name[50]; int roll; }; void Display (struct Student stu); int main() {struct Student s1; printf("Enter student's name:"); scanf("%s",s1.name); printf("Enter roll number:"); scanf("%d",&s1.roll); Display(s1); return0; } void Display(struct Student stu) { printf("OutputnName:%s",stu.name); printf("nRoll:%d",stu.roll);} Output Enter student's name: Kevin Enter roll number: 149 Output Name: Kevin Roll: 149 Example1: Passing structure by value
  • 14. void print_planet(Planet_t *pa) { printf("%sn", pa->name); //you need to use -> printf("the diameter is: %f kmn", pa->diameter); printf("number of moons : %d n", pa->moons); printf("orbit time is : %f yearsn", (*pa).orbit_time); //you can use * also } void main() { planet_t current_planet = { "pluto", 15, 7, 13, 10}; print_planet(&current_planet); } Example2: Passing structure by reference • The following function receive structure as a reference parameter to print out its contents:
  • 15. Planet_t get_planet(void) { planet_t planet; planet.name, &planet.diameter, &planet.moons, &planet.orbit_time, &planet.rotation_time); } void main() { scanf("%s%lf%d%lf%lf", return(planet); Planet_t current_planet; current_planet = get_planet(); } Example3: A function that returns a structure
  • 16.  Set of named integer constants that specifies all the legal values that a variable of its type can have. -Example: enum Color {red, white, blue} //or alternatively: //typedef enum {red, white, blue} Color; //in main function: define and initialize an enum variable: Color c; c = red; c = white; Enumeration
  • 17.  The key point to understand about an enumeration that each of the symbols stands for an integer value and can be used in any integer expression. Enumeration
  • 18. #include <stdio.h> int main() { enum Days{Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday}; Days theDay; int j = 0; printf("Please day of the week (0 to 6)n"); scanf("%d",&j); theDay = Days(j); if(theDay == Sunday || theDay == Saturday) printf("Hurray it is the weekendn"); else printf("still at work "); return 0; } Enumeration
  • 19. Union union Student { int id; float gpa; char name[20]; }; int main( ) { union Student x; printf( "Memory size occupied by x union variable: %dn", sizeof(x)); return 0; } //output: Memory size occupied by data : 20 • Union is similar to structure in definition. However, the memory allocated for a union variable is shared between union members and the size allocated is equal to the largest member size. • Example1:
  • 20. Example2: #include <string.h> union Student { int id; float gpa; char name[20]; }; int main( ) { union Student x; x.id = 10; //will use the 20 bytes x.gpa = 3.5; //will use the same 20 bytes strcpy( x.name, “Hassan Ahmed"); //will use the same 20 bytes printf( “x.id : %dn", x.id); printf( “x.gpa: %fn", x.gpa); printf( “x.name : %sn", x.name); return 0; } //output: x.id : 1917853763 x.gpa : 4122360580327794860452759994368.000000 x.name : Hassan Ahmed
  • 21. Example3: #include <stdio.h> #include <string.h> union Student { int id; float gpa; char name[20]; }; int main( ) { union Student x; x.id = 10; printf( “x.id : %dn", x.id); x.gpa = 3.5; printf( “x.gpa : %fn", x.gpa); strcpy( x.name, “Hassan Ahmed"); printf( “x.name : %sn", x.name); return 0; } //output x.id : 10 x.gpa : 3.500000 x.name : Hassan Ahmed