Structures
Structures
In our example program, the following statement declares the structure type:
struct book
{
char name ;
float price ;
int pages ;
};
This statement defines a new data type called struct book. Each variable of this data type
will consist of a character variable called name, a float variable called price and an
integer variable called pages.
structure declaration statement
struct <structure name> Once the new structure data type has been defined, one or
{ more variables can be declared to be of that type.
structure element 1 ; For example, the variables b1, b2, b3 can be declared to be
structure element 2 ; of the type struct book, as,
structure element 3 ;
struct book b1, b2, b3 ;
......
...... This statement sets aside space in memory. It makes
}; available space to hold all the elements in the structure—in
this case, 7 bytes—one for name, four for price and two for
pages.
These bytes are always in adjacent memory locations.
structure declaration statement
we can combine the declaration of the structure type and the structure variables in
one statement.
Like primary variables and arrays, structure variables can also be initialized where
they are declared.
struct book
{
char name[ 10 ] ;
float price ;
int pages ;
};
struct book b1 = { "Basic", 130.00, 550 } ;
struct book b2 = { "Physics", 150.80, 800 } ;
struct book b3 = { 0 } ;
Points in declaring a structure type:
(a) The closing brace ( } ) in the structure type declaration must be followed by a semicolon ( ; ).
(b) It is important to understand that a structure type declaration does not tell the compiler to
reserve any space in memory. All a structure declaration does is, it defines the ‘form’ of the
structure.
(c) Usually structure type declaration appears at the top of the source code file, before any
variables or functions are defined. In very large programs they are usually put in a separate
header file, and the file is included (using the preprocessor directive #include) in whichever
program we want to use this structure type.
(d) If a structure variable is initiated to a value { 0 }, then all its elements are set to value 0, as in
b3 above. This is a handy way of initializing structure variables. In absence of this, we would
have been required to initialize each individual element to a value 0.
Accessing Structure Elements
We can access individual elements of an array using a subscript. Structures use a
different scheme. They use a dot (.) operator.
Example:
b1.pages
Similarly, to refer to price, we would use,
b1.price
How Structure Elements are Stored?
/* Memory map of structure elements */
Output:-
# include <stdio.h>
Address of name = 65518
int main( )
{ Address of price = 65519
struct book Address of pages = 65523
{
char name ;
float price ;
int pages ;
};
struct book b1 = { 'B', 130.00, 550 } ;
printf ( "Address of name = %u\n", &b1.name ) ;
printf ( "Address of price = %u\n", &b1.price ) ;
printf ( "Address of pages = %u\n", &b1.pages ) ;
return 0 ;
}
Array of Structures
An array of structres in C can be defined as the collection of multiple structures variables where each variable contains
information about different entities. The array of structures in C are used to store information about multiple entities of
different data types. The array of structures is also known as the collection of structures.
Example:-
#include<stdio.h> printf("\nEnter Rollno:");
#include <string.h> scanf("%d",&st[i].rollno);
struct student{
printf("\nEnter Name:");
int rollno;
scanf("%s",&st[i].name);
char name[10];
}
};
int main(){ printf("\nStudent Information List:");
int i; for(i=0;i<5;i++){
246
369