Professional Documents
Culture Documents
Structures
Structures
Structures
Structures:
● With array, we can only declare one data type per array.
● For different data type, we need another array declaration.
● It is single type aggregate data type.
● Struct overcomes this problem by declaring composite data types which
can consist different types.
● A structure is a collection of related data items stored in one place and can
be referenced by more than one names.
● These data items are different basic data types. So, the number of bytes
required to store them may also vary.
● A structure type is a user-defined composite type.
● It is composed of fields or members which can be different types.
Structures:
● In C++, a struct is same as a class except that its members are public by
default.
● In order to use a structure, we must first declare a structure template.
● The variables in a structure are called elements or members.
● In C, you must explicitly use the struct keyword to declare a structure
however in C++, this is unnecessary, once the type has been defined.
● In C99, the allowable data types for a bit field include qualified and
unqualified _Bool, signed int, and unsigned int.
● The default integer type for a bit field is signed.
● You have the option of declaring variables when the structure type is
defined by placing one or more comma-separated variable names
between the closing brace and the semicolon.
Structures:
struct student
{
char chIdNum[5];
char chName[10];
char chGender;
int nAge;
};
Structures:
● The previous sample template for the structure can be illustrated as follow
(note the different data sizes),
Structures:
● Compiler will not reserve memory for a structure until it is declared.
● A structure declaration names a type and specifies a sequence of variable
values called 'members' or 'fields' of the structure that can have different
types.
● An optional identifier, called a 'tag', gives the name of the structure type
and can be used in subsequent references to the structure type.
● A variable of that structure type holds the entire sequence defined by that
type.
struct MyEmployee // defines a structure variable named EmpData
{ char chName[20];
int nIdNum;
long nDepatment;
} EmpData;
Syntax:
struct <struct_name>
{
<data_type><variable_name>;
<data_type><variable_name>;
….. …..
…. …….
<data_type><variable_name>;
};
struct employee
{
int emp_id;
char name[20];
float salary;
char address[50];
int age;
};
Declaring a structure variable:
A structure has to be declared after the body of structure has defined
struct <struct_name> <variable_name>;
struct employee emp1;
Here e1 contains 5 members that are defined in structure
Enter name of player Runs scored Total Runs scored by team : 311
Virat 50
Rohit 74
Dhawan 37
Shreyas 66
Rahul 35
Pant 49
1 #include<stdio.h>
2 struct player
{
3
char name[20];
4 int runs;
5 };
6 int main()
7 {
8 int i,s=0;
struct player a[11]; //a[11] - no. of players
9
printf("Enter Name of Player Runs Scored \n");
10 printf("---------------------------------------------\n\t");
11 for(i=0;i<=10;i++)
12 {
13 scanf("%s",a[i].name);
14 scanf("%d",&a[i].runs);
printf("\t");
15
}
16 for(i=0;i<=10;i++)
17 s=s+a[i].runs;
18 printf("\n---------------------------------------------\n");
19 printf("Total Runs Scored by Team: %d",s);
20 return 0;
}
21
Program 3
Write a C function to find reverse of a given sentence.
Sample input: Sample output:
How many employee info you want to accept : 3 Highest salary employee details
--------------------------------------- ------------------------------------------
Enter details for 3 employees
--------------------------------------- EMPNO NAME SALARY
Employee number : 101 105 Dev 450000
Name : John
Salary : 30000
---------------------------------------
Employee number : 105
Name : Dev
Salary : 45000
-------------------------------------
Employee number : 203
Name : Alex
Salary : 43000
1 #include <stdio.h> 1
#include<stdio.h>
2 struct employee 2 high=emp[0].salary;
for(i=0;i<n;i++)
3 { 3 {
int eno;
4 char ename[20];
4 if(emp[i].salary>high)
high=emp[i].salary;
5 int salary; 5 }
6 }emp[10]; 6 printf("Highest salary employee
int main()
7 { 7 details:");
printf("\
8 int i,high,n; 8 n-----------------------------\n");
printf("/*How many employee info\nyou want to
9 accept : ");
9 printf("EMPNO NAME SALARY\n");
for(i=0;i<n;i++)
10 printf("Enter Limit: "); 10 {
11 scanf("%d",&n); 11 if(emp[i].salary==high)
printf("-----------------------------\n");
12 printf("Enter details for %d employees:",n); 12 printf("\n %d\t%s\t
%d",emp[i].eno,emp[i].ename,emp[i].salary);
13 printf("\n-----------------------------\n"); 13 }
for(i=0;i<n;i++)
14 {
14 return 0;
}
15 printf("Employee Number: "); 15
16 scanf("%d",&emp[i].eno); 16
printf("Name : ");
17 scanf("%s",emp[i].ename); 17
18 printf("Salary : "); 18
scanf("\n %d",&emp[i].salary);
19 printf("-----------------------------\n");
19
20 } 20
21 21