Module 5 - Structure and Union
Module 5 - Structure and Union
Introduction Structure
Arrays support packing same type data together.
Many times, packing of different types of data is necessary in
programming.
E.g. A Student record may contain name which is string, id an integer,
cgpa, a float, etc.
How to store and process details of 60 students in a class?? (e.g:
name,id,cgpa)
One possible solution is:
char names[60][25];
int id[60];
float cgpa[60];
This is not an elegant solution as processing of the data is tedious and
code size will become large.
Structures in C: Provides a better way to deal with such situation.
Structures enable us to create new data types of our own..!!
Such data types are called as ‘User Defined Data types’.
Structures can combine different built-in data types to define new data
types.
4. Defining a structure
General format:
struct type-name
{
data-type1 name1;
data-type2 name2;
------------- ---------
------------- ---------
};
5. Declaring structure variables
Assume the structure is defined as:
struct mytype
{
int n;
char c;
float f;
};
Declaring variable of type mytype.
struct mytype s1,s2;
A structure type variable declaration is similar to any basic type
variable declaration Declaring structure variables - other ways
struct mytype
{
int n;
char c;
float f;
}s1,s2;
6. Accessing structure members
Accessing structure members & initialization. Members of a structure
variable is accesses using dot(.) operator.
Dot(.) operator is also referred as member operator. See the example.
struct mytype
{
int n;
char c;
float f;
}s1;
int main()
{
// accessing members & assigning values
s1.n = 10;
s1.c = 'a';
s1.f = 1.32;
// Displaying member values
printf("%d %c %f", s1.n, s1.c,s1.f);
return 0;
}
#include <stdio.h>
#include<string.h>
struct Student
{
char name[100];
char *usn;
int sem;
}
s1={"Raj","2022CS001",1};
int main()
{
strcpy(s2.name,"Steve");
s2.usn ="2022CS002";
s2.sem = 2;
In the below Program ,if we do not explicitly assign values to enum names, the
compiler by default assigns values starting from 0. For example, in the following C
program, sunday gets value 0, monday gets 1, and so on.
Example-1
#include <stdio.h>
int main()
{
// creating today variable of enum week type
enum week today;
today = Wednesday;
printf("Day %d",today+1);
return 0;}
Example-3
#include <stdio.h>
enum suit {
club = 0,
diamonds = 10,
hearts = 20,
spades = 3
} card;
int main()
{
card = club;
printf("club value is %d",card);
return 0;
}
Example-4
#include <stdio.h>
Friday, Saturday};
int main()
return 0;
}
UNION
Definition: A union is a special data type available in C that allows to store
different data types in the same memory location. You can define a union with
many members, but only one member can contain a value at any given time.
Unions provide an efficient way of using the same memory location for multiple-
purpose.
Example-1
#include <stdio.h>
#include <string.h>
union Data {
int i;
float f;
char str[20];
};
int main( ) {
return 0;
}
Example-2
#include <stdio.h>
union Job {
float salary;
int workerNo;
} j;
int main() {
j.salary = 12.3;
Example-3
#include <stdio.h>
union unionJob
{
//defining a union
char name[32];
float salary;
int workerNo;
} uJob;
struct structJob
{
char name[32];
float salary;
int workerNo;
} sJob;
int main()
{
printf("size of union = %d bytes", sizeof(uJob));
printf("\nsize of structure = %d bytes", sizeof(sJob));
return 0;}
typedef
The typedef is a keyword that is used in C programming to provide existing data
types with a new name. typedef keyword is used to redefine the name already the
existing name. When names of datatypes become difficult to use in programs,
typedef is used with user-defined datatypes, which behave similarly to defining an
alias for commands.
Example-1
#include<stdio.h>
int main ()
int a=10;
mytype b=50,sum;
sum = a + b;
return 0;
Example-2
#include<stdio.h>
#include<string.h>