Structures
- Why structures?
- As you know 'C' language provide us with variable to store the single data ,Array for storing related type of data,then Why structures ?
- Because, In real life there is a situation where we have store a data which is logical related to each other but they are not of same type.
- They are related but ,Data contains Dissimilar information,which means each data is of different datatype.
- For example you have to store a student information:
then attributes of student will be Name ,roll_no,class,div,address,etc then how can you store this data by Using int ,char or by using simple array,So this not possible by predefined datatypes. - Now , at this type of situation structures comes in action.
What are structures?
- Structures are Programmer define Data types.
- Structure is a collection of logically related data of different data types which are grouped together under a single name.
- Syntax of defining structure.
Syntax:
struct structure_name { datatype member1; // each item in structure is member datatype member2; ...... ...... };
Note:struct is a keyword.
while defining structure you have to use that keyword. - Let's define our first structure.
We will define structure for storing data of student.
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. };
- Q:Define a structure employee and it should have employee name,employee id,employee designation,and employee salary as it's members.
Definition:
struct employee //structure name { int employee_id; //member1 int variable which, //will store employee id char employee_name[20]; //member2, character array which will, //store name of employee. char employee_desig[20]; //member3,character array which will, //store designation of employee. int employee_salary; //member4, int variable which will, //store employee salary. };
- Q:Define a structure Book and it should have book name,book id ,book price,and author name as an member in it.
Definition:
struct Book //structure name { int Book_id; //member1 int variable which, //will store Book id char Book_name[20];//member2, character array which will, //store name of book. char author_name[20];//member3,character array which will, //store name of author. int Book_price; //member4, int variable which will, //store price of book. };
- The structure template ends with the semicolon ';'.
- The members of structure are enclosed in parenthesis {}.
- Now we have learned how to define a structure ,so let's use it.
- But before using we have to declare the structure.
- let's see syntax of declaring structure variable
- struct keyword is used while declaring structure variable.
- Above we have defined student data type,so declare a variable of student data type.
Declaration:
void main() { struct student s1; //s1 is a variable of student type. //as student is a datatype which is, //define by us. }
- Q:declare a variable 'emp1' of employee datatype.
employee datatype has define above.Declaration:
void main() { struct employee emp1; //emp1 is a variable of employee type. //as employee is a datatype which is, //define by us. }
- Q:declare a variable 'b1' of Book datatype.
Book datatype has define above.Declaration:
void main() { struct Book b1; //b1 is a variable of Book data type. //as Book is a datatype which is, //define by us. }
Syntax:
struct structure_name variable_name;
struct structure_name variable_name;
Further Concept:
- How to initialize an structure variable?
- How to Access Structure member?
- 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