• In my previous post I discussed about Structure and its basic concept. In this post I will discuss  how a structure can be declared and initialized and how to deal with it.
  • We can declare a structure using “struct” keyword. A structure must be declared first before using it just like all other data type. Structure can be declared by two ways.
  1. Tagged Declaration
  2. Typedef Declaration

Tagged Declaration:

  • Tagged declaration starts with the keyword “struct” followed by the tag name (structure name).
Syntax Example
struct tag_name
{
data-type var-name1;
data-type var-name2;
:
data-type var-nameN;
};
struct product
{
int pid;
char name[20];
int qnt;
float price;
};

Typedef Declaration:

  • Typedef declaration differs from tagged declaration in two ways:
  1. Keyword “typedef” is placed at the beginning of the declaration.
  2. It requires identifier at the end of structure block (“}”) and before the semicolon (;).
Syntax Example
typedef  struct
{
data-type var-name1;
data-type var-name2;
:
data-type var-nameN;
}identifier;
typedef struct
{
int pid;
char name[20];
int qnt;
float price;
} product;

Structure Variable Declaration

  • Once structure is declared we can create variables of structure type.
  • Structure variable can be declared either globally or locally.

Global declaration of structure variable:

  • When we declare structure variable outside the main () function then it becomes global and it can be accessed from anywhere within the program.
  • Structure variable can be declared using the following syntax:
struct <struct_name> var-name;
  • The following example declares global structure variable using tagged structure declaration.
Example Short hand Method
struct product
{
int pid;
char name[20];
int qnt;
float price;
};
struct product p1,p2;  // global declaration
void main()
{
// main body
}
struct product
{
int pid;
char name[20];
int qnt;
float price;
} p1,p2;
void main()
{
// main body
}
  • The following example uses typedef declaration to declare global structure variable.

Example

typedef struct
{
   int pid;
   char name[20];
   int qnt;
   float price;
} product;
product p1,p2;  // global declaration

Local declaration of structure variable:

  • Structure variable can be declared as local by declaring it inside the main () function as follow:
Tagged Declaration Example Typedef Declaration Example
struct product
{
int pid;
char name[20];
int qnt;
float price;
};
void main()
{
// Local declaration
struct product p1,p2;
}
typedef struct
{
int pid;
char name[20];
int qnt;
float price;
} product;
void main()
{
// Local declaration
   product p1,p2;
}
  • Memory representation for the structure variable can be given as shown into the below figure.

structture memory representation

  • Each structure member is allocated separate memory area.
  • Total memory required by the structure variable can be calculated as follow:
pid (int ) = 2 bytes + name (char) 20 bytes + qnt (int) 2 bytes + price(float) 4 bytes = 28 bytes

Accessing Structure Members

  • We can access individual structure members using two operators:
  1. The structure member operator (.) also called as “direct selection operator”, “dot” or “period” operator.
  2. The structure pointer operator (-> ) also called as “arrow operator”.
  • To refer to a member in a structure we need to refer to both the structure variable and structure member respectively.

Syntax :

struct-var .member-name
  • The pointer operator –consisting of a minus sign (-) and a greater than ( > ) sign without space in between.
  • It accesses a structure member via a pointer to the structure.

Initialization of Structure Variable

  • Initialization of structure variable can be done in two ways:
  1. Static initialization
  2. Dynamic Initialization

Static Initialization Example:

struct product
{
   int pid;
   char name[20];
   int qnt;
   float price;
};
void main()
{
   struct product p1,p2;
   
   // individual member initialization.
   
   p1.pid = 101 ;
   strcpy( p1.name , “Laptop” );
   p1.qnty = 10 ;
   p1.price = 35000.00 ;

   // group initialization method

   p2 = {102 , “Mobile” , 150 , 12000.00 } ;
}

Dynamic Initialization Example:


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

void main()
{
   struct product p1;
   // member initialization using scanf () function.

   printf(“\n Enter Prduct ID, Name, QNT and Price :” ) ;
   scanf(“%d %s %d %f”, &p1.pid, &p1.name, &p1.qnty, &p1.price ) ;
   printf(“%d %s %d %f”, p1.pid, p1.name, p1.qnty, p1.price ) ;

}

 

Leave a Reply

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