How to define a structure?
Keywordstruct
is used for creating a structure.Syntax of structure
struct structure_name
{
data_type member1;
data_type member2;
.
.
data_type memeber;
};
Here is an example:
struct Person
{
char name[50];
int citNo;
float salary;
};
Create structure variable
When a structure is defined, it creates a user-defined type. However, no storage or memory is allocated. To allocate memory of a given structure type and work with it, we need to create variables.Here's how we create structure variables:
struct Person
{
char name[50];
int citNo;
float salary;
};
int main()
{
struct Person person1, person2, p[20];
return 0;
}
Another way of creating a structure variable is:
struct Person
{
char name[50];
int citNo;
float salary;
} person1, person2, p[20];
In both cases, two variables person1, person2, and an array variable p having 20 elements of type struct Person are created.How to 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 structure and pointers)
person2.salary
No comments:
Post a Comment
Note: only a member of this blog may post a comment.