Structure is a user-defined
data type
The size of structure is greater than or equal to the sum of sizes of its members.
Step 1:
Step 4:
void main()
It is the collection of
different data elements
struct keyword is used to
define a structure
dot operator(.) is used
for accessing members.
When a variable is
associated with a structure, the compiler allocates the memory for each
member.
The size of structure is greater than or equal to the sum of sizes of its members.
Individual member can be
accessed at a time.
Accessing members of a structure
There are two types of operators used for accessing
members of a structure.
1. Member operator (.)
2. Structure pointer
operator(->)
Member operator (.)
Any members of a structure can be accessed by(.) operator
Any members of a structure can be accessed by(.) operator
Step 1:
Syntax:
struct[structure name]
{
{
Member definition;
Member definition;
..
Member definition
};
Step 2:
Example:
struct
Employee
{
char name[50]; //1byte memory
int
id;
//2 byte
float salary;
//4byte
}
e1;
Note:
7 bytes memory is allocated
because the compiler allocates the memory for each member.
Step 3:
Declaring structure:
struct Employee e1;
struct Employee e1;
Step 4:
Initializing
structure:
struct Employee e1={"suryosnata",1,35000.45};
struct Employee e1={"suryosnata",1,35000.45};
Step 5:
Accessing
structure members :
e1.name
e1.name
e1.id
e1.salary
Note:
You can initialize structure
in two ways, they are
1st
way:
struct Employee e1;
e1.id=1;
strcpy(e1.name, "Suryosnata");
e1.salary =35000.56;
//When you prefer 1st way
then use header file like #include<string.h>
for any string variable.
2nd
way:
struct Employee
e1={"suryosnata",1,35000.45};
Example
1(1st way):
#include <stdio.h>
#include<string.h>
#include<conio.h>
struct Employee
{
char name[50];
int id;
float salary;
};
void main()
{
struct
Employee e1;
e1.id=1;
clrscr();
strcpy(e1.name, "Suryosnata");
e1.salary =35000.45;
printf(" Name is: %s \n", e1.name);
printf(" Id is: %d \n", e1.id);
printf(" Salary is: %f \n",
e1.salary);
getch();
}
Output:
Name is: suryosnata
Id is: 1
Salary is: 35000.449219
Example 1 (2nd way):
#include <stdio.h>
#include<conio.h>
struct Employee
{
char name[50];
int id;
float salary;
};
void main()
{
struct Employee
e1={"suryosnata",1,35000.45};
printf(" Name is: %s \n",
e1.name);
printf(" Id is: %d \n",
e1.id);
printf(" Salary is: %f
\n", e1.salary);
getch();
}
Output:
Name is: suryosnata
Id is: 1
Salary is: 35000.449219
Example 2:(Taking value from user)
#include <stdio.h>
#include<conio.h>
struct
Employee
{
char name[50];
int id;
float salary;
}
e1;
void
main()
{
clrscr();
printf("Enter Employee information:\n");
printf("Enter NAME: ");
scanf("%s", e1.name);
printf("Enter ID: ");
scanf("%d", &e1.id);
printf("Enter SALARY: ");
scanf("%f", &e1.salary);
printf("Employee Information:\n");
printf("Name: %s\n",e1.name);
printf("Id: %d\n",e1.id);
printf("Salary: %f\n", e1.salary);
getch();
}
Output:
Output:
Enter Employee information:
Enter NAME: surya
Enter ID: 123
Enter SALARY: 35000
Employee Information:
Name: surya
Id: 123
Salary: 35000.000000
Structure pointer operator
(->)
If we have a pointer to structure then
members are accessed using arrows (->) operator.
Step 1:
Syntax:
struct[structure name]
{
Member definition;
Member definition;
..
Member definition
};
Step
2:
Example:
struct
Employee
{
char name[50]; //1byte memory
int
id;
//2 byte
float salary;
//4byte
}
e1;
Note:
7 bytes memory is allocated
because the compiler allocates the memory for each member.
Step
3:
Initializing
structure
struct Employee
e1={"suryosnata",1,35000.45};
Step 4:
Declaring
structure using pointer variable:
struct student *pt;
pt=&e1;
Step 5:
Accessing
structure members using pointer variable:
pt->name
pt->id
pt->id
pt->salary
Example
1:
#include <stdio.h>
#include<conio.h>
struct Employee
{
char name[50];
int
id;
float salary;
};
{
struct Employee
e1={"suryosnata",1,35000.45};
struct Employee *pt;
pt=&e1;
printf("
Name is: %s \n",pt->name);
printf(" Id is: %d \n",pt->id);
printf(" Salary is: %f
\n",pt->salary);
getch();
}
Output:
Name is: suryosnata
Id is: 1
Salary is: 35000.449219
Example
2:
#include <stdio.h>
#include<conio.h>
struct Demo
{
int
a, b;
};
void main()
{
struct Demo p1 = {143, 142};
struct
Demo *p2 = &p1;
printf("%d %d", p2->a, p2->b);
getch();
}
Output:
143 142
No comments:
Post a Comment