Structure & union
Structure means to create something.
What do you mean by the term structure?
But In c what is structure?
A structure is a convenient tool for
handling a group of logically related data
items.
Definition.....
•Structure is a collection of logically related
data items of different data types grouped
under a single name.
•Structure is analogous to records .
• As the records contain different fields, the
data items that make up a structure are called
its members or fields.
•Structure help to organize complex data is a more
meaningful way. It is powerful concept that we may after
need to use in our program Design.
•A structure is combination of different data types.
•. Lets take the example of a book, if we want to declare a
book we will be thinking about the name, title, authors and
publisher of the book and publishing year. So to declare a
book we need to have some complex data type which can
deal with more than one data types.
Now how would you defining a structure?
Here there is a syntax for defining the structure
struct structure_ name
introduces a structure
{ name of the
data_type member1; structure
data_type member2: type of declaration for the
................. Member data items
}; that make up the
structure.
Example...
struct Book
{
char Name[100];
char Author[100];
char Publisher[80];
int Year;
int Pages;
float Price;
};
The keyword struct defines a book, and each line with
in the braces defines the elements of the Book. Now
when ever we create an instance of Book it will have all
the elements of the structure i.e. Name, Author,
Publisher , Year, Pages and Price.
Declaring a Structure
Now, we have only created the format of a structure,
but we still did not declare any variable which can
store value. Thus ,there is a need to declare structure
variable so that we can use the structure member in
the program.
There are two ways to declare structure variables:
1.structure variable declaration in structure template
2.structure variable declaration any where in the
program.
Syntax....
1. type..... 2.type.......
Struct structure_ name struct structure_ name
{ {
data_type member1; data_type member1;
data_type member2; data_type member2;
................ ................
}var1,var2,......; ................
};
struct structure_ name
;
var1,var2,........;
Initializing a Structure
Initialization in structure Initialization out of structure
template template
struct book struct book
{ {
char title [15]; char title[15];
char author[10]; char author[10];
int pages; int pages;
float price; float price;
}b1={“let us c,” };
”kanetkar”,300,50.25}; struct book b1={“let us c” ,
“kanetkar” ,300,50.25};
Continue....
if there are fewer initializations than that of
member variables in the structure the
remaining member variables are initialized to
zero.
Eg :- struct book;
b1={“let us c”, “kanetkar” ,
0,150,50};
Accessing Structure Members
• Individual members of a structure can be accessed using the
dot(.) operator.
• This ‘ . ‘ operator is called as structure member operator as it
connects the structure variable and the structure member.
• Syntax:
structure _variable.structure_member;
• Example:
b.title = “Let Us C”;
b.price = 300;
printf(“The price of book is %f”,b.price);
scanf(“%f”,&b.price);
scanf(“%d”,&b.pages);
Write a C program to accept title of
book, author, pages and price using
structure and print the same
#include<stdio.h> printf(“Enter Book Title:”);
#include<conio.h> scanf(“%s”,b.title);
struct book; printf(“Enter Author Name:”);
void main() scanf(“%s”,b.author);
{ printf(“Enter No of Pages”);
struct book scanf(“%d”,&b.pages);
{ printf(“Enter Price of Book”);
char title[20]; scanf(“%f”,&b.price);
char author[20]; printf(“Book Title:%s”,b.title);
int pages; printf(“Author Name: %s:”,b.author);
float price; printf(“No of pages:%d”,b.pages);
}; printf(“Price of book:%f”,b.price);
struct book b; getch();
}
Nested structures
The individual member of a structure of can be a
structure itself. such structure declaration with some
member as structure is called nested
structure/embedded structure.
The structure to be embedded must be initially
declared as a structure and then it should be included
as the member of another structure.
Method 1
struct date
{
int month,day,year;
}dt1;
struct book
{
char title[15];
struct date;
float price;
}b1;
By using this method structure date acts as global declaration and it is
involved by all functions.
Then you can access the members of embedded structure as
follows:
b1 = {“ Harry Potter”,{12,2,2004},550.0} OR b1.dt1.day = 2; OR
printf(“%d”,b1.dt1.month);
Example:-
struct date
{
int month day, year ;
}dt1;
struct book
{
char title[15];
struct date;
float price;
}b1;
:-by using this method structure date acts as global
declaration and it is involve by all functions
Structure and function:
Structure can be passed to a function as a parameter
and also can be returned by function. This can be done
by two ways:-
1.Passing individuals Structure members
2.Passing Structure as a whole
Passing individual Structure member :
In this method, we can pass Structure member as
argument to a function call and in this process ,these
individual Structure members are treated as separate
non- Structure values
Passing Structure as a whole:
In this method ,we can pass the entire Structure to a
function by simply providing the name of the
Structure variable as the argument in the function call.
Structure and pointer:
Pointer to Structure:
Till now , we have learn pointer to variables and
pointer to arrays. In a similar manner , we can also
have pointer to Structure. This pointer points to the
starting address of the Structure. We can declare a
pointer to Structure using ‘&’ operator.
Example...
struct book
{
char title[15];
char author[10];
int pages;
float price;
}b1, *bptr; //* bptr is a pointer to struct book
bptr = &b1;
Pointer within a Structure
A Structure can contain pointer as member.
Example:-
{
char title[15];
char *author;
int *pages;
float price;
}b1;
Such a pointer members can be accessed as below:
b1. pages=120;
b2. author = “sharuneel”;
Unions:
Unions like Structure are the user defined data types
and somewhat similar to Structure as they also contain
member of different data types.
Since the member of union share the same memory,
only one member can be active at a time.
Thus union are useful as they efficiently use the
computer’s memory.
Declaring union:
Syntax:- Example:-
union union_name union book
{ {
data_type member1; char title[15];
data_type member2; char *author;
............... int pages;
}var1,var2,...; float price;
}b1,b2,b3;
Difference between Structure and unions:
Unions Structure
All members in the union Each member in the
share the same storage area in Structure is assigned its own
the computer’s memory. unique storage area.
Allocation memory equal to Allocation the memory equal
the maximum memory to total memory required by
required by the member. the members.
Only one member can be All member can be active at a
active at a time time.
Only the first member of a All member of a Structure
union variable can be variable can be initialized
initialized.
Continue....
Conservation of memory. Occupies lot of memory
space.
Example:- assume Example:-assume
union uu Struct ss
{ int a; { int a;
float b; float b;
char c; char c;
}; };
then size of ss would be
then size of uu(union) >7bytes (compiler
dependent-if int , float, char
Would be 4 bytes
are taken as 2,4,1)