Initializing and Accessing

                     structure Member

  • As we have learned how to declare and define the structure, now it's time for initializing the structure and Accessing Element of structure.
  • Let's start doing it.
Let's define a structure Student with name , rollno,  div as it's attribute.

Definition:
struct student  //structure name
{
    int  rollno; //member1 ,integer variable which will store,
                 //student roll no.
                 
    char  name[20]; //member2 a character array of 20 character
                    //which will store name of student.
                    
    char  div;  //member3 a character variable,
              //which will store division of student.
};

As we have define structure let's initialize it.

Initializing the variable s1
void main()
{

 struct student s1={1,"Roy","A"};  
 //initialized  structure student.
         //s1 is a variable of student type.;
        //as student is a new data type  which is,
        //define by us.
//student s1
//roll no = 1, name=Roy , div = A                     


}

For accessing member of structure we use '.'dot operator.

Syntax: for accessing element

structure_variable_name.member_name;


NOw let's access the element of initialized variable s1 of student datatype.

Accessing rollno
void main()
{
int  s_rollno;

//Accessing the member "roll no" of  s1 variable.
s_rollno = s1.rollno;

// assigning rollno of s1 student to variable s_rollno
}

Accessing name
void main()
{
int  s_name[20];

//Accessing the member "name" of  s1 variable.
strcpy(s_name,s1.name);
// assigning name of s1 student to character array s_name 
}

Accessing div
void main()
{
int  s_div;

//Accessing the member "div" of  s1 variable.
s_div = s1.div;
// assigning div of s1 student to character  variable s_div
}


Let's define a structure employee with  employee_id , name, salary as it's attribute.

Definition:
struct employee  //structure name
{
    int  employee_id; //member1 ,integer variable which will store,
                 // employee id.
                 
    char  name[20]; //member2 a character array of 20 character
                    //which will store name of employee.
                    
    int  salary;  //member3 a integer variable,
              //which will store salary of employee.
};

As we have define structure let's initialize it.

Initializing the variable E1
void main()
{

 struct student E1={20,"Roy",500000};  
 //initialized  structure employee.
         //E1 is a variable of employee type.;
        //as employee is a new data type  which is,
        //define by us.
//employee E1
//employee_id = 20, name=Roy , salary = 500000                     


}

NOw let's access the element of initialized variable E1 of student datatype.

Accessing employee_id
void main()
{
int  emp_id;

//Accessing the member "employee_id" of  E1 variable.
emp_id = E1.employee_id;

// assigning employee_id of E1 employee to variable emp_id
}

Accessing name
void main()
{
char  e_name[20];

//Accessing the member "name" of  E1 variable.
strcpy(e_name,E1.name);
// assigning name of E1 employee to character array s_name 
}

Accessing salary
void main()
{
int  e_salary;

//Accessing the member "employee_Salary" of  s1 variable.
e_salary = E1.salary;
// assigning salary of E1 employee to integer variable e_salary
}


Let's define a structure book with book_id , book_name, book_price as it's attribute.

Definition:
struct book  //structure name
{
    int  book_id; //member1 ,integer variable which will store,
                 // book id.
                 
    char  book_name[20]; //member2 a character array of 20 character
                    //which will store name of book.
                    
    int  book_price;  //member3 a integer variable,
              //which will store price of book.
};

As we have define structure let's initialize it.

Initializing the variable B1
void main()
{

 struct book B1={10,"learn c",200};  
 //initialized  structure book.
         //B1 is a variable of book type.;
        //as book is a new data type  which is,
        //define by us.
//book B1
//book_id = 10, name="learn c" , book_price = 200                     


}

NOw let's access the element of initialized variable B1 of employee datatype.

Accessing book_id
void main()
{
int  b_id;

//Accessing the member "book_id" of  B1 variable.
b_id = B1.book_id;

// assigning book_id to variable b_id
}

Accessing name
void main()
{
char  b_name[20];

//Accessing the member "book_name" of  B1 variable.
strcpy(b_name,B1.book_name);
// assigning book name of B1 book to character array b_name 
}

Accessing Price
void main(),
{
int  b_price;

//Accessing the member "employee_Salary" of  s1 variable.
b_price = B1.book_price;
// assigning price of B1 book to integer variable b_salary
}

  • This is how we Access and initialize the structure.
  • Do more practice for having good command on structure.
  • They are used in data structures Like Tress, linked list etc





Further Concept:


People Also Searched: