STRUCTURE
•STRUCTURE
•UNIONS
1. INTRODUCTION
C’ Supports a constructed data type known
as STRUCTURE, Which is a method of
packing data of different types.
WHAT IS STRUCTURE ?

WHAT IS STRUCTURE ?
A Structure is a convenient tool for handling
a group of logically related data items.
i.e. It can be used to represent a set of
attributes, such as : student_name , roll_no


A structure defination creates a
formet that may be used to declare
structure variables.
i.e. Consider a book database
i.e. Consider a book database
consisting of a book name, Author,
Numbe of pages and price.
We can define a structure to hold this
information as follows:
Struct tag_name
{
data_type
data_type
…
member1;
member2;
…
…
………
…
…..
};
1. The template terminated with a semicolon
2. While the entire declarationn considered as
satement, each member is declared
indepandently for its name and type in a
a
indepandently for its name and type in a
separate statement inside the teplate.
3. The tag name such as book can be used to
declare structure variable later in the program
Struct book
{
Char title[20];
Char author[15];
Int pages;
Int pages;
Float price;
};
The keyword struct declares a structure to
hold the details of four fields, namely
title,author,pages and price.
These fields are called structure elements or
members and each member belong to
members and each member belong to
different type of data.
Book is the name of the structure and
also called ‘STRUCTURE TAG’.
title
author
pages
price
Array of 20 characters
Array of 15
characters
integer
float
Declaring structure variable
After defining structure format we can declare variable
of data type. Structure variable declaration is similar to the
declaration of variables of any other data type.
It includes the following element.
The keyword struct.
Structure big-name
List of variable names separated by commas.
List of variable names separated by commas.
Terminating semicolon (;)
Eg: the statement
Struct book a,b,c;
Declare a,b,c as variables of type struct book. Each one of
these variables has 4 members as specified in the
definition complete declaration
The link between a member and variable is
established using the member operator ‘.’
which is also known as ‘dot operator’ or
‘period operator’.
i.e.
Accessing structure Members
i.e.
book1.price
Is the variable represnting the price of book1
and can be treated like any other ordinary
variable.
Here is how we would assign values
to
members of book1:
strcpy(book1.title, “COMPUTER”);
strcpy(book1.author, “XYZ”);
the
strcpy(book1.author, “XYZ”);
book1.pages=250;
book1.price=29.99;
We can also use scanf to give the
values
through keyboard.

structure and union1.pdf

  • 1.
  • 2.
    1. INTRODUCTION C’ Supportsa constructed data type known as STRUCTURE, Which is a method of packing data of different types. WHAT IS STRUCTURE ?  WHAT IS STRUCTURE ? A Structure is a convenient tool for handling a group of logically related data items. i.e. It can be used to represent a set of attributes, such as : student_name , roll_no  
  • 3.
    A structure definationcreates a formet that may be used to declare structure variables. i.e. Consider a book database i.e. Consider a book database consisting of a book name, Author, Numbe of pages and price. We can define a structure to hold this information as follows:
  • 4.
  • 5.
    1. The templateterminated with a semicolon 2. While the entire declarationn considered as satement, each member is declared indepandently for its name and type in a a indepandently for its name and type in a separate statement inside the teplate. 3. The tag name such as book can be used to declare structure variable later in the program
  • 6.
    Struct book { Char title[20]; Charauthor[15]; Int pages; Int pages; Float price; };
  • 7.
    The keyword structdeclares a structure to hold the details of four fields, namely title,author,pages and price. These fields are called structure elements or members and each member belong to members and each member belong to different type of data. Book is the name of the structure and also called ‘STRUCTURE TAG’.
  • 8.
    title author pages price Array of 20characters Array of 15 characters integer float
  • 9.
    Declaring structure variable Afterdefining structure format we can declare variable of data type. Structure variable declaration is similar to the declaration of variables of any other data type. It includes the following element. The keyword struct. Structure big-name List of variable names separated by commas. List of variable names separated by commas. Terminating semicolon (;) Eg: the statement Struct book a,b,c; Declare a,b,c as variables of type struct book. Each one of these variables has 4 members as specified in the definition complete declaration
  • 10.
    The link betweena member and variable is established using the member operator ‘.’ which is also known as ‘dot operator’ or ‘period operator’. i.e. Accessing structure Members i.e. book1.price Is the variable represnting the price of book1 and can be treated like any other ordinary variable.
  • 11.
    Here is howwe would assign values to members of book1: strcpy(book1.title, “COMPUTER”); strcpy(book1.author, “XYZ”); the strcpy(book1.author, “XYZ”); book1.pages=250; book1.price=29.99; We can also use scanf to give the values through keyboard.