Structures in C Programming by T Koki
Structures in C Programming by T Koki
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