Lecture 9 - Custom Data Types v1
Lecture 9 - Custom Data Types v1
Lecture-9
Custom Data Type
Courtesy:
Nusrat Jahan Salim
Md. Asaduzzaman Sourov
Md. Moyeenul Hossain Ratul
structure
union
enumeration
• A structure is a user defined data type that groups logically related data items of
different data types into a single unit, for convenient handling.
• A variable of structure type can store multiple data items of different data types
under the one name. As the data of employee in company that is name, Employee
ID, salary, address, phone number is stored in structure data type.
• An array allows you to store similar data types together while a structure is different in the sense
that it allows you to store different data types together.
* If there are fewer initializers in the list than members in the structure, the
remaining members are automatically initialized to 0 (or NULL if the member
is a pointer).
* Structure variables defined outside a function definition (i.e., externally) are
initialized to 0 or NULL if they are not explicitly initialized in the external
definition.
• The parentheses are needed here because the structure member operator (.) has a higher precedence than the
pointer dereferencing operator (*).
main( )
{ We can’t use ptr.name or ptr.callno because ptr
struct book is not a structure variable but a pointer to a
{ structure
char name[25] ;
char author[25] ;
int callno ;
};
struct book b1 = { "Let us C", "YPK", 101 } ;
struct book *ptr ; an arrow operator(->) must be used to refer to
ptr = &b1 ; the structure elements in case of ‘structure
printf ( "\n%s %s %d", b1.name, b1.author, b1.callno ) ; pointers’
printf ( "\n%s %s %d", ptr->name, ptr->author, ptr->callno ) ;
}
struct ME union me
{ {
char student_name[6]; char student_name[6];
int roll; int roll;
float marks; float marks;
char grade; char grade;
}student_1; }student_2;
. .. .. . … … . . .. .. . .
. . . .. .. .
enum product washroom; washroom is a variable which takes its value as shampoo
washroom= shampoo;
void main()
{
enum product{
shampoo, soap, toothpaste=134, toothbrash
};
enum product washroom;
washroom= shampoo;
printf("shampoo:%d,soap:%d,toothpaste:%d,toothbrash:%d", shampoo, soap, toothpaste, toothbrash);
}
Memory Allocates separate memory for Allocates a shared memory space for No memory is allocated; it uses
Allocation each member. the largest member. constants.
Size Sum of sizes of all members. Size is equal to the size of the No memory size as it only assigns
largest member. names to integers.
Access All members can be accessed Only one member can be accessed Each enumeration value
simultaneously. at a time (due to shared memory). represents a unique constant.
Use Case Used to store and manipulate Used to save memory when only Used to improve code readability
multiple related variables of one value is needed at a time. and manage constant values.
different types.
Example struct Person { int age; char union Data { int i; float f; }; enum Days { MON, TUE, WED
name[50]; }; };
- Socrates