Explain different types of structure declaration with examples

Types of Structure Declaration

  1. Tagged Declaration
  2. Typedef Declaration

Tagged Declaration

Tagged declaration starts with the keyword “struct” followed by the tag name (structure name).

Syntax

struct tag_name
{
data-type var-name1;
data-type var-name2;
:
data-type var-nameN;
};

Example:-

struct Person{ 
char name[10]; 
int age; 
float salary; 
};

Typedef Declaration

The structure definition associated with keyword typedef is called Type-Defined Structure.

Where,

  • typedef is the keyword used at the beginning of the definition and by using typedef user defined data type can beobtained.
  • struct is the keyword which tells structure is defined to the complier
  • The members are declare with their data_type
  • Type_name is not a variable, it is user defined data_type.

Syntax:

typedef struct 
{ 
data_type member 1; 
data_type member 2; 
……………………… 
……………………… 
data_type member n; 
}Type_name; 

Example:-

typedef struct
{
int pid;
char name[20];
int qnt;
float price;
} product;

Leave a Reply

Your email address will not be published. Required fields are marked *