Creating Structures and Structure variables

By
Advertisement
C Language provides a number of ways to store group of elements using different derived data types like arrays, structures,. etc. An array is a homogeneous collection of variables. But there may be a need to store all the non homogeneous elements into a single group, this can be achieved using structures. 

                      structures are group of different types of variables under a single name. In C language structures can be defined in two ways. Using tagged struct element and typedef.

Tagged declaration is done using the keyword "struct" as follows
 
struct book

{

char tile[20];

int pages;

float prices;

};
 
 
The elements title,pages, and prices are called structure members. It is terminated by a semicolon.

Type-def structures 

Typedef  struct 
{
char tile[20];
int pages;
float prices;
 }type;

The only difference in above two difference is  that the typedef deceleration needs a type variable at the end and we can't create a typedef variable like structure variables.

Creating Structure Variable.


                                      Without  Structure Variable the structure members are invalid that is there is no of these members. The structure variable can be declared in any of the following two methods.In the first method, Structure Variable can be created at the time of deceleration as 


struct book
{
char tile[20];
int pages;
float prices;
}bk;

             Here the "bk" is a  Structure Variable that can be used to access and manipulate structure members.

In the second method, Structure Variable can be created at the time of deceleration as

struct book
{
char tile[20];
int pages;
float prices;
};

for this structure we will create structure variable as 

struct book bk;
  

0 comments:

Post a Comment