Declaration of Structures

To declare a structure you must start with the keyword struct followed by the structure name or structure tag and within the braces the list of the structure’s member variables. Note that the structure declaration does not actually create any variables. The syntax for the structure declaration is as follows:

struct structure-tag
{
datatype variable1;
datatype variable2;
dataype variable 3;

};

For example, consider the student database in which each student has a roll number, name and course and the marks obtained. Hence to group this data with a structure-tag as student, we can have the declaration of structure as:

struct student
{
int roll_no;
char name[20];
char course[20];
int marks_obtained ;
};

The point you need to remember is that till this time no memory is allocated to the structure. This is only the definition of structure that tells us that there exists a user-defined data type by the name of the student which is composed of the following members. Using this structure type, we have to create the structure variables:

struct student stud1, stud2 ;

At this point, we have created two instances or structure variables of the user-defined data type student. Now memory will be allocated. The amount of memory allocated will be the sum of all the data members which form part of the structure template.

The second method is as follows:

Struct
{
int roll_no;
char name[20];
char course[20];
int marks_obtained ;
} stud1, stud2 ;

In this case, a tag name student is missing, but still it happens to be a valid declaration of the structure. In this case, the two variables are allocated memory equivalent to the members of the structure.

The advantage of having a tag name is that we can declare any number of variables of the tagged named structure later in the program as per requirement.

If you have a small structure that you just want to define in the program, you can do the definition and declaration together as shown below. This will define a structure of type struct telephone and declare three instances of it.

Consider the example for declaring and defining a structure for the telephone billing with three instances:

struct telephone
{
int tele_no;
int cust_code;
char cust_address[40];
int bill_amt;
} tele1, tele2, tele3;

The structure can also be declared by using the typedefinition or typedef.

This can be done as shown below:

typedef struct country
{
char name[20];
int population;
char language[10];
} Country;

This defines a structure which can be referred to either as struct country or Country, whichever you prefer. Strictly speaking, you don’t need a tag name both before and after the braces if you are not going to use one or the other. But it is a standard practice to put them both in and to give them the same name, but the one after the braces starts with an uppercase letter.
The typedef statement doesn’t occupy storage: it simply defines a new type. Variables that are declared with the typedef above will be of type struct country, just like population is of type integer. The structure variables can be now defined as below:

Country Mexico, Canada, Brazil;

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: