0% found this document useful (0 votes)
7 views9 pages

Unit 7

Structures in C allow the grouping of multiple logically related variables of different data types into a single entity, facilitating the management of complex data like employee or book details. They can be declared using the 'struct' keyword, and structure variables can be created to access and modify their members using the dot operator. Additionally, arrays of structures enable efficient handling of multiple entities, improving code reusability and readability.

Uploaded by

jeetamadhar5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views9 pages

Unit 7

Structures in C allow the grouping of multiple logically related variables of different data types into a single entity, facilitating the management of complex data like employee or book details. They can be declared using the 'struct' keyword, and structure variables can be created to access and modify their members using the dot operator. Additionally, arrays of structures enable efficient handling of multiple entities, improving code reusability and readability.

Uploaded by

jeetamadhar5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Unit 7

Structure
At times, during programming, there is a need to store multiple logically related elements
under one roof. For instance, an employee’s details like name, employee number, and
designation need to be stored together. In such cases, the C language provides structures to do
the job for us.

A structure can be defined as a single entity holding variables of different data types that are
logically related to each other. All the data members inside a structure are accessible to the
functions defined outside the structure. To access the data members in the main function, you
need to create a structure variable.

Why Do We Use Structures in C?

Structure in C programming is very helpful in cases where we need to store similar data of
multiple entities. Let us understand the need for structures with a real-life example. Suppose
you need to manage the record of books in a library. Now a book can have properties like
book_name, author_name, and genre. So, for any book you need three variables to store its
records. Now, there are two ways to achieve this goal.

The first and the naive one is to create separate variables for each book. But creating so many
variables and assigning values to each of them is impractical. So what would be the
optimized and ideal approach? Here, comes the structure in the picture.

We can define a structure where we can declare the data members of different data types
according to our needs. In this case, a structure named BOOK can be created having three
members book_name, author_name, and genre. Multiple variables of the type BOOK can be
created such as book1, book2, and so on (each will have its own copy of the three members
book_name, author_name, and genre).
Syntax to Define a Structure in C

struct structName

// structure definition

Data_type1 member_name1;

Data_type2 member_name2;

Data_type2 member_name2;

};

Below is the description of Structure in C programming

Description of the Syntax

 Keyword struct: The keyword struct is used at the beginning while defining a structure in
C. Similar to a union, a structure also starts with a keyword.

 structName: This is the name of the structure which is specified after the keyword struct.

 data_Type: The data type indicates the type of the data members of the structure. A
structure can have data members of different data types.

 member_name: This is the name of the data member of the structure. Any number of data
members can be defined inside a structure. Each data member is allocated a separate space
in the memory.

How to Declare Structure Variables?

Variables of the structure type can be created in C. These variables are allocated a separate
copy of data members of the structure. There are two ways to create a structure variable in C.

 Declaration of Structure Variables with Structure Definition

This way of declaring a structure variable is suitable when there are few variables to be
declared.
Syntax

struct structName

// structure definition

Data_type1 member_name1;

Data_type2 member_name2;

Data_type2 member_name2;

} struct_var1, struct_var2;

Example

struct bookStore

// structure definition

char storeName

int totalBooks;

char storeLicense[20];

} storeA, storeB; // structure variables

The structure variables are declared at the end of the structure definition, right before
terminating the structure. In the above example, storeA and storeB are the variables of the
structure bookStore. These variables will be allocated separate copies of the structure’s data
members that are- storeName, totalBooks, and storeLicense.
How to Initialize Structure Members?

Structure members cannot be initialized like other variables inside the structure definition.
This is because when a structure is defined, no memory is allocated to the structure’s data
members at this point. Memory is only allocated when a structure variable is declared.
Consider the following code snippet.

struct rectangle

// structure definition

int length = 10; // COMPILER ERROR: cannot initialize members here.

int breadth = 6; // COMPILER ERROR: cannot initialize members here.

};

A compilation error will be thrown when the data members of the structure are initialized
inside the structure.

To initialize a structure’s data member, create a structure variable. This variable can access
all the members of the structure and modify their values. Consider the following example
which initializes a structure’s members using the structure variables.

struct rectangle

// structure definition

int length;

int breadth;

};

int main()

{
struct rectangle my_rect; // structure variables;

my_rect.length = 10;

my_rect.breadth = 6;

In the above example, the structure variable my_rect is modifying the data members of the
structure. The data members are separately available to my_rect as a copy. Any other
structure variable will get its own copy, and it will be able to modify its version of the length
and breadth.

How to Access Structure Elements?

The members of a structure are accessed outside the structure by the structure variables using
the dot operator (.). The following syntax is used to access any member of a structure by its
variable:

Syntax

structVariable.structMember

Example

The following example illustrates how to access the members of a structure and modify them
in C.

#include <stdio.h>

#include <string.h>

struct cube

// data members

char P_name[10];

int P_age;
char P_gender;

};

int main()

// structure variables

struct cube p1, p2;

// structure variables accessing the data members.

strcpy(p1.P_name, "XYZ");

p1.P_age = 25;

p1.P_gender = 'M';

strcpy(p2.P_name, "ABC");

p2.P_age = 50;

p2.P_gender = 'F';

// print the patient records.

// patient 1

printf("The name of the 1st patient is: %s\n", p1.P_name);

printf("The age of the 1st patient is: %d\n", p1.P_age);

if (p1.P_gender == 'M')

printf("The gender of the 1st patient is: Male\n");

}
else

printf("The gender of the 1st patient is: Female\n");

printf("\n");

// patient 2

printf("The name of the 2nd patient is: %s\n", p2.P_name);

printf("The age of the 2nd patient is: %d\n", p2.P_age);

if (p2.P_gender == 'M')

printf("The gender of the 2nd patient is: Male\n");

else

printf("The gender of the 2nd patient is: Female\n");

return 0;

Introduction to Array of structure in C


An array can be defined as a data structure where we can group the variables of the same data
types. Each element of the array can be char, int, double, float or even a structure. We know a
structure allows elements of diverse data types to be grouped under a single name. We can
think of this new structure as a new data type in itself. Thus, an array can comprise elements
of this new data type and is called array of that structure/ new data type.

Declaration of Array of Structure in C


Structure is a data type that gives us the ability to allow a group of interconnected variables to
be thought of as one unit instead of distinct entities. A structure may contain different data
types – char, int, double, float, etc. It may also include an array as its member.

The struct keyword is used to create a structure. Here is an example:

struct student
{
char name[50];
char class[100];
int roll_number;
float marks[5];
};

Why to Use an Array of Structures


The main advantage of an array is we can represent multiple values with a single variable. So
the reusability of code improves; also, readability is increased. If there is no array of
structures, we need to store many values in multiple structure variables, which is redundant.

For example, We don't need to remember structure variables like:

struct student s1, s2, s3;

Instead, we can use an array of structures like:

struct student s[3];

Before, we created 3 different variables to store 3 students. Now all this can be done
efficiently in one single array.

Hence, C enables us to declare an array of structures. We can evade declaring the different
structure variables

What is an Array of Structures in C


An array of structures written in C can be described as a collection of numerous structure
variables containing data about different entities. It is used to hold information about various
entities of diverse data types. The array of structures can also be described as a collection of
structure variables.

As you can see in the below image, S is array of structure student having size 10 which
means it could store information of 10 different variables of type student. So we don't need to
take 10 different variables instead we could use array of structure student.

Conclusion
 A Structure is a data type that gives us the ability to allow a collection of
interconnected variables to be treated as a single unit instead of distinct entities.
 There are two ways of declaring the structure variable i.e with and without the
structure declaration.
 An array of structure is much more efficient than declaring multiple variables
separately.
 An array of structures written in C can be described as a collection of numerous
structure variables containing data about different entities.

You might also like