Programming with C

⌘K
  1. Home
  2. Docs
  3. Programming with C
  4. Structures
  5. What is Structure

What is Structure

A structure is a collection of variables under a single name and provides a convenient way of grouping several pieces of related information together. It can be used for storing heterogeneous data (data of different types)

Three aspects of working with structures:

  • Defining a structure type, i.e. creating a new type
  • Declaring variables and constants (i.e. objects) of the newly created type
  • Using and performing operations on the objects of the structure type

Structure Definition

struct structure-name
{
   data_type1 member1;
   data_type2 member2;
   data_type3 member3;
};
  • struct keyword is used to declare structure.
  • structure-name is user defined name. It is a valid identifier. It is otherwise known as structure-tag name
  • structure declaration list is enclosed within braces. It consists of one or more variables possibly of different data type. The variables declared in the structure declaration list are termed as structure members or fields. Structure members can be variables of the basic types (char, int, float), pointer types (char *) or aggregate types (array and structures)

Example

struct student
{
	char name[20];
	int rollno;
	int m1,m2,m3;
	int total;
	float average;	
};

Structure definition does not reserve any memory space for the structure members in the data segment. Thus, it is not possible to initialize the structure members during the structure definition

If a structure definition does not contain a structure tag-name, the created structure is unnamed. The unnamed structure type is also known as anonymous structure type. It is not possible to declare its objects after its definition. Thus, the objects of unnamed or anonymous structure type should be declared only at the time of structure definition.

Declaring Structure Objects

Structure Definition

When a structure is defined, it creates a user-defined type but, no storage or memory is allocated. When creating object or structure variable, then memory space will be allocated.

Syntax to create structure objects is,

struct structure-name
{
   data_type1 member1;
   data_type2 member2;
   data_type3 member3;
}o;
struct structure-name o1,o2;

Here o,o1,o2 represents structure variables.

Example

struct student
{
	char name[20];
	int rollno;
	int m1,m2,m3;
	int total;
	float average;	
}s1,s2;

Accessing structure Member

Structure members can be accessed using

  • Member operator (.)
  • Indirect member access operator or arrow operator (->)

Syntax

structure-name. member;

Example

s1.rollno;

Displaying student information using structures

#include<stdio.h>
struct student
{
	char name[10];
	int m1,m2,m3;
	int total;
	float average;
};
int main()
{
	struct student s,s1;
	printf("Enter name \n");
	scanf("%s",s.name);
	printf("Enter marks \n");
	scanf("%d%d%d",&s.m1,&s.m2,&s.m3);
	s.total = s.m1+s.m2+s.m3;
	s.average = s.total / 3;
	printf("student Details \n");
	printf("Name:%s\n",s.name);
	printf("Student marks:%d\t%d\t%d\n",s.m1,s.m2,s.m3);
	printf("Total:%d\n",s.total);
	printf("Average:%f\n",s.average);
	printf("Enter name \n");
	scanf("%s",s1.name);
	printf("Enter marks \n");
	scanf("%d%d%d",&s1.m1,&s1.m2,&s1.m3);
	s1.total = s1.m1+s1.m2+s1.m3;
	s1.average = s1.total / 3;
	printf("student Details \n");
	printf("Name:%s\n",s1.name);
	printf("Student marks:%d\t%d\t%d\n",s1.m1,s1.m2,s1.m3);
	printf("Total:%d\n",s1.total);
	printf("Average:%f\n",s1.average);	
}

Output

C:\TDM-GCC-64>gcc 5_Simple_Structure.c -o 5ss
C:\TDM-GCC-64>5ss
Enter name
abc
Enter marks
90
90
100
student Details
Name:abc
Student marks:90        90      100
Total:280
Average:93.000000
Enter name
qwe
Enter marks
89
90
91
student Details
Name:qwe
Student marks:89        90      91
Total:270
Average:90.000000

Initialization of structure variable

#include<stdio.h>
struct student
{
	int stu_id;
	char stud_name[10];
	int mark1;
	int mark2;
	float avg;
};
int main()
{
	struct student s= {101,"ABC",90,100};
	s.avg = (s.mark1 + s.mark2) / 2;
	printf("****Details are:****\n");
	printf("Student Id:%d\n",s.stu_id);
	printf("Student Name:%s\n",s.stud_name);
	printf("Mark 1: %d\n",s.mark1);
	printf("Mark 2: %d\n",s.mark2);
	printf("Average:%f\n", s.avg);
	return 0;
}

Output

C:\TDM-GCC-64>gcc 4_initialization.c -o 4i
C:\TDM-GCC-64>4i
****Details are:****
Student Id:101
Student Name:ABC
Mark 1: 90
Mark 2: 100
Average:95.000000	

Views: 0

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments