0% found this document useful (0 votes)
15 views20 pages

C (Structures)

Uploaded by

Vidhi Gaba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views20 pages

C (Structures)

Uploaded by

Vidhi Gaba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Structures in C

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 ;

 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.
Variable declaration of structure
◦ If we so desire, we can combine the declaration of the
structure type and the structure variables in one statement.
 For example: struct book
{
float price ;
char name ;
int pages ;
};
struct book b1, b2, b3 ;
is same as...
struct book
{
char name ;
float price ;
int pages ;
} b1, b 2,b3;
Structure initialization
struct book
{
char name[10] ;
float price ;
int pages ;
};
struct book b1 = { "Basic", 130.00, 550 } ;
struct book b2 = { "Physics", 150.80, 800 } ;
Accessing Structure Elements
◦ Structures use a dot (.) operator to
access the structure elements. In our
program we have to use
b1.pages
◦ Similarly, to refer to price we would use
b1.price
Note :Before the dot there must always be a
structure variable and after the dot there
must always be a structure element.
How Structure Elements are Stored

The elements of a structure are always


stored in contiguous memory locations as
shown in the Figure :

b1.name b1.price b1.pages

‘B’ 130.0 550

1001 1002 1006


Array of Structures
In our program, to store data of 100 books
we would be required to use 100 different
structure variables from b1 to b100,
which is definitely not very convenient.
A better approach would be to use an
array of structures. Following program
shows how to use an array of structures.
/* Usage of an array of structures */
main( )
{
struct book
{
char name ;
float price ;
int pages ;
};
struct book b[100] ;
int i ;
for ( i = 0 ; i <= 99 ; i++ )
{
printf ( "\nEnter name, price and pages " ) ;
scanf ( "%c %f %d", &b[i].name, &b[i].price, &b[i].pages ) ;
}
for ( i = 0 ; i <= 99 ; i++ )
printf ( "\n%c %f %d", b[i].name, b[i].price, b[i].pages ) ;
}
Copying and comparing structure variables
The values of a structure variable can
be assigned to another structure
variable of the same type using the
assignment operator. It is not necessary
to copy the structure elements piece-
meal. For example.
main( )
{
e2.salary = e1.salary ;
struct employee /* copying all elements at one
{ go */
char name[10] ; e3 = e2 ;
printf ( "\n%s %d %f",
int age ;
e1.name, e1.age, e1.salary ) ;
float salary ;
printf ( "\n%s %d %f",
};
e2.name, e2.age, e2.salary ) ;
struct employee e1 = { "Sanjay", 30,
5500.50 } ; printf ( "\n%s %d %f",
struct employee e2, e3 ; e3.name, e3.age, e3.salary ) ;
/* piece-meal copying */ }
strcpy ( e2.name, e1.name ) ;
e2.age = e1.age ;
Nested structure

One structure can be nested within another structure.


main( )
{
struct address

{
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;

You might also like