Structure :
Structure in c is a user-defined data type that enables us to store the collection of different data types. Each element of a structure is called a member.
The struct keyword is used to define the structure.
Syntax :
struct [structure Name] {
member definition;
member definition;
...
member definition;
};
OR
struct [structure Name] {
member definition;
member definition;
...
member definition;
} obj;
e.g
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
} book;
Access members of a structure
There are two types of operators used for accessing members of a structure.
.
- Member operator->
- Structure pointer operator (will be discussed in the next tutorial)
book.name or book->name
Program :
Memory Management for Array Of Structure :
Why structs in C?
Suppose, you want to store information about a person: his/her name, citizenship number, and salary. You can create different variables name, citNo and salary to store this information.
What if you need to store information of more than one person? Now, you need to create different variables for each information per person: name1, citNo1, salary1, name2, citNo2, salary2,etc.
A better approach would be to have a collection of all related information under a single name Person
structure and use it for every person.
No comments:
Post a Comment