4 Structure
In Array we can store data of one type only, but structure is a variable that gives facility of storing data of different data type in one variable.
Structures are variables that have several parts; each part of the object can have different types.
Each part of the structure is called a member of the structure.
# Declaration
Consider basic data of student :
roll_no, class, name, age, address.
A structure data type called student can hold all this information:
struct student {
int roll_no
char class
char name[25];
int age;
char address[50];
};
before the final semicolon, At the end of the structure's definition, we can specify one or more structure variables.
There is also another way of declaring variables given below,
struct student s1;
# Initialization
Structure members can be initialized at declaration. This is same as the initialization of arrays; the values are listed inside braces. The structure declaration is preceded by the keyword static.
static struct student akki ={1234,''comp'',''akki'',20,'' mumbai''};
# Accessing structure data
To access a given member the dot notation is use. The ''dot'' is called the member access operator
struct student s1;
s1.name = ''Akki'';
s1.roll_no = 1234
# scope
A structure type declaration can be local or global, depending upon where the declaration is made.
# Structures as Function Arguments
You can pass a structure as a function argument in very similar way as you pass any other variable or pointer. You would access structure variables in the similar way as you have accessed in the above example
# Pointers to Structures
You can define pointers to structures in very similar way as you define pointer to any other variable as follows:
struct student *struct_pointer;
In Array we can store data of one type only, but structure is a variable that gives facility of storing data of different data type in one variable.
Structures are variables that have several parts; each part of the object can have different types.
Each part of the structure is called a member of the structure.
# Declaration
Consider basic data of student :
roll_no, class, name, age, address.
A structure data type called student can hold all this information:
struct student {
int roll_no
char class
char name[25];
int age;
char address[50];
};
before the final semicolon, At the end of the structure's definition, we can specify one or more structure variables.
There is also another way of declaring variables given below,
struct student s1;
# Initialization
Structure members can be initialized at declaration. This is same as the initialization of arrays; the values are listed inside braces. The structure declaration is preceded by the keyword static.
static struct student akki ={1234,''comp'',''akki'',20,''
# Accessing structure data
To access a given member the dot notation is use. The ''dot'' is called the member access operator
struct student s1;
s1.name = ''Akki'';
s1.roll_no = 1234
# scope
A structure type declaration can be local or global, depending upon where the declaration is made.
# Structures as Function Arguments
You can pass a structure as a function argument in very similar way as you pass any other variable or pointer. You would access structure variables in the similar way as you have accessed in the above example
# Pointers to Structures
You can define pointers to structures in very similar way as you define pointer to any other variable as follows:
struct student *struct_pointer;
No comments:
Post a Comment