Structure and Pointers
Introduction
• Arrays require that all elements be of the homogenous (same) data
type.
• Many times it is necessary to group information of different data
types.
• For example if the programmer has to implement the student
management system, he would want all the information like a student's
department, faculty number, enrolment number, class serial number,
etc, and store them together.
• A Structure is a collection of related data types, possibly of different
types.
Structures
• A structure is a collection of variables of different data types under a
single name.
• The variables are called members of the Structure.
• It includes the following elements:
1. The keyword struct.
2. The structure tag name.
3. List of variable names separated by commas.
4. A terminating semicolon.
Structure Tag
Syntax Example
struct [structure tag] { struct Student {
member definition; char faculty_no[50];
member definition; Members of char enrolment_no[50];
structure.
... char department[100];
member definition; int roll_no;
} [one or more structure };
variables];
In this example the structure Student do not have a structure variable. It gives
the compiler the layout or information of structure, but does not actually create
one for use. Therefore memory space is not reserved.
Structure Tag
Syntax Example
struct [structure tag] { struct Student {
member definition; char faculty_no[50];
member definition; Members of char enrolment_no[50];
structure.
... char department[100];
member definition; int roll_no;
} [one or more structure }st1, st2, st3;
variables];
In this example the structure Student have a structure variable. The members of
a structure occupy memory when they are associated with a structure_variable.
The data will be accessed through this variable.
• 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.faculty_no, st1.enrolment_no is member variable name of st1
structure variable while st3.faculty_no, st3.enrolment_no is member variable of
st3 structure variable.
• Below shows format of a structure
faculty_no enrollment_no department roll_no
st1
st2
st3
Array of Structures
• Similar to creation of arrays other data types like int and float, the programmer can
create array of structures.
• It is declared like this(based on above declaration of Struct student):
Student students[10];
• Each of the members may be accessed as a normal array with index. For example:
students[1].faculty_no;
students[2].faculty_no;
faculty_no enrollment_no department roll_no
student[1]
student[2]
……
……
student [10]
Nested Structure
Nested Structure
Pointers
• A pointer is a derived data type in C.
• It is built from one of the fundamental data types available in C.
• Pointers contain memory addresses as their values.
• Since these memory addresses are the locations in the computer memory where
program instructions and data are stored, pointers can be used to access and
manipulate data stored in the memory.
Pointers
‘*’ operator
• The `*´ is an indirection operator or `value at address operator´.
• In simple words, we can say that if address of a variable is known,
then the ‘*’ operator provides the contents of the variable.
Pointers
• An address of a variable can be stored in a special variable called pointer variable.
• A pointer variable contains the address of another variable.
• A pointer variable y can be declared in ‘C’ as shown below:
int *p;
where:
p is the name of the pointer
* indicates to be a pointer
int is the type of data being pointed by p