Thursday, November 26, 2020

Structure

A structure is a user defined data type in C. A structure creates a data type that can be used to group items of possibly different types into a single type.

structure is user defined data type available in C that allows to combine data items of different kinds.

A structure is a collection of heterogeneous (different) data items into a single type. Each element of a structure is called a member. 

Defining a Structure

struct keyword is used to define the structure.

structure in c :

The syntax to define the

struct structure_name  

    data_type member1; 

    data_type member2; 

    . 

    . 

    data_type memeberN; 

};

Example:

struct employee 

{  

    int id; 

    char name[20]; 

    float salary; 

}; 

The following image shows the memory allocation of the structure employee that is defined in the above example.



sizeof(employee) = 4+10+4 bytes = 18 bytes

Declaring structure variable

A variable for the structure has to be declared to access the member of the structure easily.

There are two ways to declare structure variable:

  1. By struct keyword within main() function
  2. By declaring a variable at the time of defining the structure.

First Method:

 struct employee 

    int id; 

    char name[50]; 

    float salary; 

};

 After defining structure write below code inside the main() function.

 struct employee e1, e2; 

 The variables e1 and e2 can be used to access the values stored in the structure.

 Second Method:

 struct employee 

{  

    int id; 

    char name[50]; 

    float salary; 

}e1,e2; 

If number of variables is not fixed, use the first method. It provides the flexibility to declare the structure variable as many times as required

Accessing members of the structure

There are two ways to access structure members:

  1. By . (member or dot operator)
  2. By -> (structure pointer operator)

A pointer can be created to a structure. If a pointer to structure is created, members are accessed using arrow ( -> ) operator.

The code to access the id member of e1 variable by . (dot) operator.

e1.id

Example 1:

#include<stdio.h> 

#include <string.h>   

struct employee     

{   int id;     

    char name[50];     

}e1;  //declaring e1 variable for structure   

void  main( )   

{   

   //store first employee information   

   e1.id=101;   

   strcpy(e1.name, "Ashraf");//copying string into char array   

   //printing first employee information   

   printf("employee1 id : %d\n", e1.id);   

   printf("employee1 name : %s\n", e1.name);   

}  

Example 2:

#include<stdio.h>

struct Point

{

   int x, y;

};

void  main()

{

   struct Point p1 = {1, 2};

   //p2 is a pointer to structure p1

   struct Point *p2 = &p1;

   // Accessing structure members using structure pointer

   printf("%d %d", p2->x, p2->y);

}

0 comments:

Post a Comment

Data Structures with C++



NET/SET/CS PG



Operating Systems



Computer Networks



JAVA



Design and Analysis of Algorithms



Programming in C++

Top