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.
};
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
}
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;
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
}
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
}
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
}
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.
};
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
}
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
}
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
}
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
}
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.
};
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
}
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
}
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
}
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
}
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:
- What are Nested Structure?
- How to built an Nested Structure?
- How to Access members of Nested Structure?
- How to initialize Nested structure?
- Functions and Structure
- How to pass a structure variable to function?
- How to make function which will take structure variable as arguments?
- How to make function which will return structure variable?
- Array and Structure
- How to make array of structure variable?
- How to initialize array structure variable?
- How to Access members of an array of structure?
- How to take members of array structure as input from the user?
- Pointer and Structure
- How to Point a structure variable?
- How to declare a pointer of structure variable?
- How to Initialize an pointer of structure variable?
- How to access member of structure variable which is point by an pointer?
- What is '->' Operator?
- How to allocate memory for structure variable Dynamically
- How to allocate memory of structure variable using malloc() and calloc() function
- Self Referential Block
- What is Referential Structure?
- How to Create self Referential Structure?
- Applications of Self Referential Structures
- Practice Programs
People Also Searched:
- What are structures?
- Why structures?
- Need of Structure
- Concept of structure
- Array and Pointer
- What is array of pointer?
- how to initialize an array of pointers?
- How to Access elements of an Array Of Pointers?
- Functions and Pointers
- How to pass a Pointer To a Function?
- Call By reference
- Functions In C
- Why Functions?
- What are Functions?
- how to write our functions?
- What are programmer define functions?
- How to declare function?
- How to define a function?
- What is function Prototype?
- how to declare a function?
- what are Actual Parameters?
- What are Formal Parameters?
- Function Call?
- how to call a Function?
- What are different types of calling function?
- Call by value
- call by reference
- Recursion
- Array and Functions
- How to pass an array to a function?
- What is Character array
- Functions for Characters
- Functions from #include<ctype.h> Header File
- what are isdigit(), islower() , isupper(),etc
- what are various string handling functions
- Functions from #include<string.h> Header File
- what are strcpy(), strcmp(), strncmp(), strcmpi(),etc
- strcmp() VS strncmp() VS strcmpi()
- Input And Output Functions
- How to take strings As Input
- What is gets() functions
- What is puts() function
0 Comments
Post a Comment