¤ Home » Programming » C Tutorial » Introduction to Structures in C

Introduction to Structures in C

The struct data type is a commonly used data structure in C programming language. Unlike the data structure array, whose elements are all of the same data type, the data structure struct may comprise of individual elements of different data types. Thus, a single structure may contain integer, float and character elements.

A structure can contain elements of type pointer, arrays and even other structures too. The individual structure elements are referred to as members. Hence, in a structure declaration we also need to include the definition of each of its members that constitute the structure.


Structure Declaration

The composition of a structure may be defined as follows:

struct tag  {
	member 1;
	member 2; 
	. . .
	member n;
};

In, this declaration, the keyword struct declares a structure of type tag and member 1, member 2, till member n are the members of the structure. It must be noted that two members of the same structure cannot have the same name. It should also be noted that separate storage classes cannot be assigned to the members. The storage class if defined for the structure will apply to all its elements. Again, the elements of a structure cannot be independently initialized.

The struct declaration defines a structure of type tag that is simply a template for which no memory is yet allocated. Once you have defined a struct data type, you can then declare one or more variables of the defined struct type. To create variables (for which memory space would be allocated) of the structure of type tag, the following declaration syntax is used:

struct tag variable_1, variable_2, variable_n; 

To get a more clear idea let us look at an example.

struct student { 
	int   enrolment_no;
	char  name[21]; 
	char  course_name[11]; 
	float module_marks[4];
	char  grade; 
};

struct student medical, engineering; 

Here the structure student consists of five members, viz. enrolment number (of integer type), name (character string), course name (which is a string of length 10), marks (a floating point array that maintains marks attained in four lecture modules) and grade. Two variables are defined as medical and engineering of type student. The above struct definition and declaration can be combined and also written in the following way:

struct student { 
	int   enrolment_no;
	char  name[21]; 
	char  course_name[11]; 
	float module_marks[4];
	char  grade; 
} medical, engineering; 

Now each member of the structure student can be accessed using the declared variable name(s) such as medical, engineering followed by the member name separated by a period, as variable.member.

For instance, if you need to work with enrolment_no of medical student, you will refer to it as medical.enrolment_no. Note that the period (.) is an operator with highest precedence. Similarly, marks for the second module of an engineering student can be addressed as engineering.module_marks[1], and so on.

The members of a structure can be assigned some initial values in a manner similar to the way we did it, in case of an array. Note that a structure variable can be initialized only if its storage class is either extern or static.

static struct student medical = { 
	10001, "Arunav Mishra", "MBBS", 99.00, 78.50, 67.77, 75.55, 'B' 
};

Note: The array element module_marks has been declared to be of 4 sub-elements. Hence, while initializing, we must take care to provide all 4 marks values, viz 99.00, 78.50, 67.77, 75.55 in this case.


Try this code.

#include 
main(int argc,char *argv[])
{
	struct student { 
		int   enrolment_no;
		char  name[21]; 
		char  course_name[11]; 
		float module_marks[4];
		char  grade; 
	};

	static struct student medical = { 
		10001, "Arunav Mishra", "MBBS", 99.00, 78.50, 67.77, 75.55, 'B' 
	};

	printf("Student Name: %s, Course: %s\n", medical.name, medical.course_name);
	printf("Module Marks: %5.2f %5.2f %5.2f %5.2f\n", medical.module_marks[0], medical.module_marks[1], medical.module_marks[2], medical.module_marks[3]);
	printf("Grade: %c\n", medical.grade);
} 

We have mentioned earlier that one or more members of a structure can themselves be structures as well. See this example declaration.

struct name { 
	char first_name[21];
	char middle_name[21];
	char last_name[21]; 
};

struct student { 
	int   enrolment_no;
	struct name stud_name; 
	char  course_name[11]; 
	float module_marks[4];
	char  grade; 
} medical, engineering; 

It is also possible to define an array of structures where each element of the array is a structure. Here is how you can declare an array of 40 students of type struct student.

struct student class[40];

To access the details of the first student in the class, viz. first element of the student array, we use the following combination of variable and member names.

class[0].enrolment_no           - to refer to the enrolment_no of first student
class[0].stud_name.first_name   - to refer to the first name of first student

The n-th student's data can thus be accessed as...

class[n-1].enrolment_no
class[n-1].stud_name.first_name

... and so on.

The above array of structures can be assigned initial values in the following manner.

static struct student class[] = {
   10002, "suvir", "Prakash", "Sinha", "Electronics", 100.00, 99.56, 89.99, 81.50, 'A',
   10003, "Anirvan", "", "Bhaskar", "Electrical", 95.78, 76.55, 90.00, 91.50, 'A'
};


LAB WORK

Write a complete C program that will accept the following information for each team participating in a football tournament.

  1. Team name, including the city (e.g. Mohan Bagan, Kolkata)
  2. Number of wins
  3. Number of losses
  4. Number of draws
  5. No. of goals scored
  6. No. of goals conceded

Enter the data for all participating teams. Then, reorder and print the information about the tournament by listing teams according to their performance, i.e. number of wins. If two teams have same number of wins, order them according to their number of losses.



Share:




comments powered by Disqus


Buy Domain & Hosting from the most reliable and trusted company - WebServicesWorldWide.com.

Looking to build a website?
Launch a 20 page website in 1 day at only Rs.200/month or US$ 3.19/month. Hosting & Email included.





About the Author

Rajeev Kumar
CEO, Computer Solutions
Jamshedpur, India

Rajeev Kumar is the primary author of How2Lab. He is a B.Tech. from IIT Kanpur with several years of experience in IT education and Software development. He has taught a wide spectrum of people including fresh young talents, students of XLRI, industry professionals, and govt. officials.

Rajeev has founded Computer Solutions & WebServicesWorldwide.com, and has hands-on experience of building variety of web applications and portals, that include - SAAS based ERP & e-commerce systems, independent B2B, B2C, Matrimonial & Job portals, and many more.



Copyright © How2Lab.com. All rights reserved.

Refer a friend | Sitemap | Disclaimer | Privacy Policy