STRUCTURES in C

  • Structure in c is a collection of multiple data elements that can be of different data types
  • The Array is a collection of the data item of the same type, while the structure is equal to the memory space required by all the elements independently to be stored in memory
  • The memory space required to store one variable of the structure is equal to the memory space required by all the element independently to be stored in memory
  • A structure declaration that is not followed by a list of variables reserves no storage; it merely describes a template or shape of a structure
syntax of structure:
struct structure_name
{
data_type variable_name;
data_type variable name;
-
-
}
  • The variables declared inside the structure are called as the member elements of the structure student
  • A structure member or tag and an ordinary(i.e non-member) variable can have the same name without conflict since they can always be distinguished by context
  • Furthermore, the same member names may occur in different structures, although as a matter of style one would normally use the same names only for closely related objects
  • Structure in C is normally used when a set of data items corresponding to common things are to be stored together
  • For example, when information of a student is to be stored, we need to store his name, roll number, fees, etc.
  • In this case, we can make a structure element and store the make a structure element and store the corresponding data of every student in a separate structure element
  • To access a member element of a structure variable we use the dot operator (“.”), also called as the period operator
  • It is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling
  • Structures help to organize complicated data, particularly in large programs, because they permit a group of related variables to be treated as a unit instead of as separate entities

Display the name, matches played and goals scored by hockey player using structure.

#include<stdio.h>
#include<conio.h>
struct hockey
{
char name[20];
int matches,goals;
}cl;// declaration of variable of structure
void main()
{
clrscr();
printf("Enter the hockey player's name,matches played and goals scored:\n");
scanf("%s",c1.name);
scanf("%d"&c1.matches);
scanf("%d",&c1.goals);
printf("the hockey player's details are as follows:\nName:%s\nMatches played:%d\nGoals scored:%d\n",c1.name,c1.matches,c1.goals);
getch();
}
OUTPUT:
Enter the hockey player's name,matches played and goals scored:
Abhishek
67
41
the hockey player's details are follows:
Name:Abhishek
Matches played:67
Goals scored:41

Translate »