1) The document discusses structures in C programming. A structure is a collection of variables of different data types grouped together under a single name.
2) Structures are defined using the struct keyword followed by the structure name and members. Multiple variables of a structure type can be declared. Members are accessed using the dot operator.
3) Pointers to structures can be declared like pointers to other variables. Structure members can also be accessed using pointers. Arrays of structures allow storing multiple structures in an array. Structures can be nested by defining a structure inside another structure.
3. Structure
• A structure is a collection of variables of
different data types under a single name.
• The variables are called members of the
structure.
• The structure is also called a user-defined
data type.
3
4. Defining a Structure
• Syntax:
struct structure_name
{
data_type member_variable1;
data_type member_variable2;
………………………………;
data_type member_variableN;
};
Note: The members of a structure do not occupy
memory until they are associated with a
structure_variable.
4
5. struct student
{
char name[20];
int roll_no;
float marks;
char gender;
long int phone_no;
};
•Multiple variables of struct student type can be
declared as:
struct student st1, st2, st3; 5
Example
6. Defining a structure…Defining a structure…
• Each variable of structure has its own
copy of member variables.
• The member variables are accessed using
the dot (.) operator or member operator.
• For example: st1.name is member
variable name of st1 structure variable
while st3.gender is member variable
gender of st3 structure variable.
6
9. Pointers to structurePointers to structure
A pointer is a memory address.
You can define pointers to
structures in very similar way as
you define pointer to any other
variable
Prepared By Soummo Spriya
151-15-4741
11. • Referencing pointer to
another address to access
memory
• Using dynamic memory
allocation
Structure's member through
pointer can be used in two ways:
12.
13. ““Structure within anotherStructure within another
StructureStructure
(Nested Structure)”(Nested Structure)”
Md.Masumer Rahman
Id: 151-15-5040
14. What is Nested Structure
• Nested structure in C is nothing but structure within
structure. One structure can be declared inside other
structure as we declare structure members inside a
structure. The structure variables can be a normal
structure variable or a pointer variable to access the
data.
• Nested Structures are allowed in C Programming
Language.
• We can write one Structure inside another structure
as member of another structure.
16. Way 1: Declare two separate structures
struct date
{
int date;
int month;
int year;
};
struct Employee
{
char ename[20];
int ssn;
float salary;
struct date doj;
}
emp1;
17. Way 2 : Declare Embedded structures
struct Employee
{
char ename[20];
int ssn;
float salary;
struct date
{
int date;
int month;
int year;
}
doj;
}
emp1;
20. Function and Structure
We will consider four cases
here:
Passing the individual member to
functions
Passing whole structure to functions
Passing structure pointer to functions
Passing array of structure to functions
21. Passing the individual member to
functions
Structure members can be
passed to functions as actual
arguments in function call
like ordinary variables.
22. PASSING WHOLE STRUCTURE TO
FUNCTIONS
Whole structure can be passed to a function
by the syntax:
function _ name ( structure _ variable _
name );
The called function has the form:
return _ type function _ name (struct tag _ name
structure _ variable _ name)
{
…………;
}
23. Passing structure pointer to
functions
In this case, address of structure
variable is passed as an actual argument
to a function.
The corresponding formal argument must
be a structure type pointer variable.
24. Passing array of structure to function
Passing an array of structure type to a
function is similar to passing an array of
any type to a function.
That is, the name of the array of
structure is passed by the calling function
which is the base address of the array of
structure.
Note: The function prototype comes after
the structure definition.
26. Arrays of structures
• An ordinary array: One type of data
• An array of structs: Multiple types of data
in each array element.
0 1 2 … 98 99
0 1 2 … 98 99
27. Arrays of structures
• We often use arrays of structures.
• Example:
StudentRecord Class[100];
strcpy(Class[98].Name, “bangladeshi man");
Class[98].Id = 12345;
strcpy(Class[98].Dept, "COMP");
Class[98].gender = 'M';
Class[0] = Class[98]; Bangladeshi man
12345 M
COMP
. . .
0 1 2 … 98 99
28. Arrays inside structures
• We can use arrays inside structures.
• Example:
struct square{
point vertex[4];
};
square Sq;
• Assign values to Sq using the given square
x y x y x y x y
(4, 3) (10, 3)
(4, 1) (10, 1)