Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
structures in C and Union in C, enumerated datatype
1.
2. Definition of structures
• A structure is a collection of logically related
elements having a single name.
Syntax:
struct tag_name
{
type1 member1;
type2 member2;
……………
} empty or variable;
struct student
{
char name[10];
Int roll_number;
} s1;
3. Initialization of structures
• Example
struct student
{
char name[5];
int roll_number;
} s1={“ravi”,10};
struct student
{
char name[5];
int roll_number;
};
struct student s1= {“ravi”,10};
4. Accessing structures
• Structures use a dot (.) operator to refer
to its elements also known as period
operator.
• Syntax:
structure_variablename.structure_membername
5. Example for accessing structures
struct student
{
char name[5];
int roll_number;
};
struct student s1= {“ravi”,10};
Accessing members using variables as:
s1.name refers to string “ravi”
s1.roll_number refers to roll_number 10
6.
7. Definition of union
• Union is a collection of variables referred
under a single name.
• Syntax and declaration of union is similar to a
structure but its functionality is different.
• In structures, each member has its own
storage whereas all members of union use
the same location.
8. Syntax and example
union tag_name
{
type1 member1;
type2 member2;
……………
} empty or variable;
union u
{
char c;
int i;
} a;
Accessing members
a.c
a.i
9.
10. Definition
• An enumerated data type is a user defined data
type which includes descriptive names also called
enumerators.
• Syntax
enum type_name
{
member1;
member2;
……………
} empty or variable;
enum color
{
RED;
BLUE;
GREEN;
} c;
11.
12. Definition
• A pointer is a variable that points to a memory
location in which data is stored.
• Example: if v is a variable stored in memory,
then to access the location of v, we define
another variable pv.
pv= &v;
Address of v Value of v
pv v
13. • In order to access the data of variable v, then
*pv is used, where * is unary operator, called
indirection operator.
• For example: if we take another variable u,
then to access the value we use:
pv= &v;
u= *pv;