Introduction to Structures
At the end of this lecture you should be able to:.
• Define the term structure.
• Give the syntax of a structure definition.
• Define structure variables or instances.
• Access structure members
• Define arrays of structures.
Tuesday, June 10, 2025 Lecturer :Mr C Zano
Structures
• A Structure is a collection of related variables of
different data types referenced by a single name.
• Unlike arrays where elements are of the same data
type, here the variables are of different data types
and elements of a structure are called members.
• They are commonly used to define records to be
stored in files.
• Structures are derived data types which act like
other built in types like int, float or char.
Tuesday, June 10, 2025 Lecturer :Mr C Zano
Defining Structures in C
• A Structure definition declares its members in braces and is
terminated by a semi-colony.
• Syntax
• struct name-of-structure
{
Members of the structure
};
• The definition is introduced by the key word struct, followed by
the name of structure.
• Members of a structure must have unique names and can be
variables of basic data types e.g. (int, char, float) or aggregates
such as arrays and other structures.
Tuesday, June 10, 2025 Lecturer :Mr C Zano
Defining Structures in C
Example
• struct student
{
char Regno [8];
char firstname[20];
char lastname[20];
int Age;
char gender;
char programme[50];
};
We can then define variables of type struct student, e.g. struct student
student1; in the main function.
Tuesday, June 10, 2025 Lecturer :Mr C Zano
Defining Structures in C
A structure cannot contain an instance of itself. i.e. a variable of type struct
student cannot be declared in the definition for struct student, however we
may include a pointer to struct student.
Example
• struct student
{
char Regno [8];
char firstname[20];
char lastname[20];
int Age;
char gender;
char programme[50];
struct student stud; //error
struct student * studptr; //correct
};
Tuesday, June 10, 2025 Lecturer :Mr C Zano
Creating Structures instances
To create an instance of a structure, you can append a
variable name after the close curly brace and before the
semi-colon in the structure definition e.g.
struct example
{
char c;
int i;
} ex1, ex2;
• Or you can create it like other variables, with the type of
the variable being the word struct followed by the name
of the structure. e.g. struct example ex3;
Tuesday, June 10, 2025 Lecturer :Mr C Zano
Accessing Structures members
Two operators are used
1. The structure member operator(.)- also
called the dot operator.
2. The structure pointer operator ( ->) – also
called the arrow operator.
Tuesday, June 10, 2025 Lecturer :Mr C Zano
Accessing Structures members
The structure pointer operator ( ->)
• The structure pointer operator consist of a
minus sign – followed by a greater than > sign
with no intervening spaces.
• It accesses a structure member via a pointer to
the structure.
• Remember a pointer to a structure can be
declared as a member inside the structure
definition or as other instances outside.
Tuesday, June 10, 2025 Lecturer :Mr C Zano
Accessing Structures members
• Assume the pointer aptr has been declared for a structure
say student with a member regno, to print the regno with
aptr we use
• printf(“%s”,aptr ->regno);
• The expression aptr ->regno is equivalent to
• (*aptr).regno, which dereferences the pointer and accesses
the member regno using the structure member operator.
• The paranthesis are needed here because the member
operator (.) has a higher precedence than the pointer
dereferencing operator (*).
Tuesday, June 10, 2025 Lecturer :Mr C Zano