Structures are used when you want to
process data of multiple data types but you still want to refer to the data as
a single entity. Structures are similar to records in Cobal or Pascal. For
example, you might want to process information on students in the categories of
name and marks (grade percentages). Here you can declare the structure
‘student’ with the fields ‘name’ and ‘marks’, and you can assign them
appropriate data types. These fields are called members of the structure. A
member of the structure is referred to in the form of structurename.membername.
Program
struct student
\\ A
{
char
name[30]; \\ B
float
marks; \\ C
} student1,
student2; \\ D
main ( )
{
struct
student student3; \\ E
char
s1[30]; \\ F
float
f; \\ G
scanf
("%s", name); \\ H
scanf
(" %f", & f); \\ I
student1.name = s1; \\ J
student2.marks = f; \\ K
printf
(" Marks are %f \n", student2.marks); \\ M
}
Explanation
- Statement A defines the
structure type student. It has two members: name and marks.
- Statement B defines the
structure member name of the type character 30.
- Statement C defines the
structure member marks of the type float.
- Statement D defines two
structure variables: structure1 and
structure2. In the program you have to
use variables only. Thus struct student is the data type, just as int and student1 is the variable.
- You can define another
variable, student3, by using the notations as
specified in statement E.
- You can define two local
variables by using statements F and G.
- Statement J assigns s1 to a member of the
structure. The structure member is referred to as structure variablename.membername. The member student1.name is just like an ordinary
string, so all the operations on the string are allowed. Similarly,
statement J assigns a value to student1.marks
- Statement L prints the marks
of student1 just as an ordinary string.
Points to Remember
- Structures are used when you
want to process data that may be of multiple data types.
- Structures have members in
the form: structurename.membername.
You can
define structures of arrays or arrays of structures, etc. The following section
gives definitions of complex structures.
Program
Struct address
\\ A
{
plot char
[30], struc char[30];
city
char[30]
}
struct student
\\ B
{
name
char[30];
marks
float;
struct
address adr; \\ C
}
main ( )
{
struct
student student1; \\ D
struct
student class[20]; \\ E
class[1].marks = 70; \\ F
class[1].name = " Anil ";
class[1].adr.plot = "7 "; \\ G
class[1].adr.street = " Mg Road";
class[1].adr.city = "mumbai";
printf(
" Marks are %d\n", class[1].marks);
printf(
" name are %s\n", class[1].name);
printf(
" adr.plot is %s\n", class[1].adr.plot);
printf(
" adr.street is %s\n", class[1].adr.stret);
printf(
" adr.city is %s\n", class[1].adr.city);
}
Explanation
- Statement A declares the
address of a structure containing the members plot, street and city.
- Statement B declares a
structure having 3 members: name, marks, and adr. The data type of adr is structure address, which
is given by statement C.
- Statement D defines the
variable student1 of the data type struct student.
- Statement E defines an array
class with 20 elements. Each element is a structure.
- You can refer to marks of
the students of class[1] using the notation class[1].marks. class[1] indicates the first element
of the array, and since each element
is a structure, a member can be accessed using dot notation.
- You can refer to the plot of
a student of class[1] using the notation class[1].adr.plot. Since the third element of
the structure is adr, and plot is a member of adr, you can refer to members
of the nested structures.
- If you want to refer to the
first character of the character array plot, then you can refer it as
8. Class[1].adr.plot[0]
because plot is a
character array.
Points to Remember
- When a structure is a member
of another structure it is called a nested structure.
- You can define structures of
arrays or arrays of structures, and the members are referred to using dot
notations.
For each
structure, variable memory is allocated. The following sections give the memory
layout of the structure student1.
Program/Example
student1
student1 0 name
30
marks
34
adr plot
64
street
94 city
Explanation
- Suppose the base address of the allocations is
0; then the first member name
starts from 0.
- Since name has 30 characters, the
second member, marks, starts from location 30; marks occupies 4 bytes.
- The third member, adr, starts from location 34,
so the first member of adr
starts from location 34. Period plot occupies 30 bytes, so street starts at 64.
- city starts at 94.
- You can print the addresses
of the members using the following printf statements:
6. printf( "16lu\n", &student1.marks);
7. printf( "16lu\n",
&student1.adr.plot);
Point to Remember
The
structure members are allocated consecutive memory locations.