C (Structures)
C (Structures)
Structure
We want to store the following data about three books. -
name, price and number of pages in it. The following
example illustrates this scenario:
main( )
{
char name[3] ;
float price[3] ;
int pages[3] ;
printf ( "\nEnter names, prices & no. of pages of 3 books\
n" ) ;
for (i=0;i<=2;i++)
scanf ( "%c %f %d", &name[i], &price[i],
&pages[i] ) ;
printf ( "\nAnd this is what you entered" ) ;
for (i=0;i<=2;i++)
printf ( "%c %f %d", name[i], price[i], pages[i] ) ;
}
Structure
A structure contains a number of data types grouped
together. These data types may or may not be of the same
type. The following example illustrates the use of this data
type.
main( )
{
struct book
{
char name ;
float price ;
int pages ;
};
struct book b1, b2, b3 ;
printf ( "\nEnter names, prices & no. of pages of 3 books\n" ) ;
scanf ( "%c %f %d", &b1.name, &b1.price, &b1.pages ) ;
scanf ( "%c %f %d", &b2.name, &b2.price, &b2.pages ) ;
scanf ( "%c %f %d", &b3.name, &b3.price, &b3.pages ) ;
printf ( "\nAnd this is what you entered" ) ;
printf ( "\n%c %f %d", b1.name, b1.price, b1.pages ) ;
printf ( "\n%c %f %d", b2.name, b2.price, b2.pages ) ;
printf ( "\n%c %f %d", b3.name, b3.price, b3.pages ) ;
}
Declaring a Structure
struct <structure name>
{
structure element 1 ;
structure element 2 ;
.....
};
For example
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.
Variable declaration of structure
Once the new structure data type has been defined,
one or more variables can be declared to be of that
type. For example the variables b1, b2, b3 can be
declared to be of the type struct book, as,
struct book b1, b2, b3 ;
{
char phone[15] ;
char city[25] ;
int pin ;
};
struct emp
{
char name[25] ;
struct address a ;
};
struct emp e = { "jeru", "531046", "nagpur", 10 };
printf ( "\nname = %s phone = %s", e.name, e.a.phone ) ;
printf ( "\ncity = %s pin = %d", e.a.city, e.a.pin ) ;
}
Passing of individual structure elements to functions
main( )
{
struct book
{
char name[25] ;
char author[25] ;
int callno ;
};
struct book b1 = { "Let us C", "YPK", 101 } ;
display ( b1.name, b1.author, b1.callno ) ;
}
display ( char *s, char *t, int n )
{
printf ( "\n%s %s %d", s, t, n ) ;
}
Passing the entire structure variable at a time
It can be immediately realized that to pass individual
elements would become more tedious as the number of
structure elements go on increasing. A better way would be
to pass the entire structure variable at a time. This method is
shown in the following program.
struct book
{
char name[25] ;
char author[25] ;
int callno ;
};
main( )
{
struct book b1 = { "Let us C", "YPK", 101 } ;
display ( b1 ) ;
}
display ( struct book b )
{
printf ( "\n%s %s %d", b.name, b.author, b.callno ) ;
}
Structure pointer
Let us look at a program that demonstrates the usage of a
structure pointer.
main( )
{
struct book
{
char name[25] ;
char author[25] ;
int callno ;
};
struct book b1 = { "Let us C", "YPK", 101 } ;
struct book *ptr ;
ptr = &b1 ;
printf ( "\n%s %s %d", b1.name, b1.author, b1.callno ) ;
printf ( "\n%s %s %d", ptr->name, ptr->author, ptr->callno )
;
/* Passing address of a structure variable */
struct book
{
char name[25] ;
char author[25] ;
int callno ;
};
main( )
{
struct book b1 = { "Let us C", "YPK", 101 } ;
display ( &b1 ) ;
}
display ( struct book *b )
{
printf ( "\n%s %s %d", b->name, b->author, b->callno ) ;
}
Unions
Likestructures, but every member occupies the
same region of memory!
◦ Structures: members are “and”ed together: “name
and species and owner”
◦ Unions: members are “xor”ed together
union VALUE {
float f;
int i;
char *s;
};
/* either a float xor an int xor a string */
Unions
Storage
size of union is the size of its largest member
avoid unions with widely varying member sizes;
for the larger data types, consider using pointers
instead
Initialization
◦ Union may only be initialized to a value appropriate
for the type of its first member
Example of the Use of a Union
union temperature
{
short int surfaceOfEarthTemperature;
long int astronomicalTemperature;
float floatingPointTemperature;
};
union temperature celsiusTemperature, fahrenheitTemperature,
ovenTemperature,
surfaceOfTheSunTemperature;
main()
{
celsiusTemperature.floatingPointTemperature = 87.3;
fahrenheitTemperature.floatingPointTemperature =
32.0 + (9.0 * celsiusTemperature.floatingPointTemperature/5.0);
ovenTemperature.ssurfaceOfEarthTemperature = 375;
surfaceOfTheSunTemperature.astronomicalTemperature = 4387912;