0% found this document useful (0 votes)
40 views

C PDF

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

C PDF

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

EXERCISE 10

Content:
1. C Structure
2. typedef in C
3. C Array of Structures
4. C Nested Structure

I. C Structure
Why use structure?
In C, there are cases where we need to store multiple attributes of an entity.
It is not necessary that an entity has all the information of one type only. It
can have different attributes of different data types. For example, an entity
Student may have its name (string), roll number (int), marks (float). To store
such type of information regarding an entity student, we have the following
approaches:
 Construct individual arrays for storing names, roll numbers, and marks.
 Use a special data structure to store the collection of different data
types.
What is Structure?
Structure in c is a user-defined data type that enables us to store the collection
of different data types. Each element of a structure is called a member.
Structures ca; simulate the use of classes and templates as it can store various
information
The ,struct keyword is used to define the structure. Let's see the syntax to
define the structure in C.

struct structure_name
{
data_type member1;
data_type member2;
.
.
data_type memeberN;
};

1
Let's see the example to define a structure for an entity employee in C:

struct employee
{ int id;
char name[20];
float salary;
};

The following image shows the memory allocation of the structure employee
that is defined in the above example:

Here, struct is the keyword; employee is the name of the structure; id,
name, and salary are the members or fields of the structure. Let's
understand it by the diagram given below:

2
Declaring structure variable
We can declare a variable for the structure so that we can access the member
of the structure easily. There are two ways to declare structure variable:
 By struct keyword within main() function
 By declaring a variable at the time of defining the structure.
1st way:
Let's see the example to declare the structure variable by struct keyword. It
should be declared within the main function.

struct employee
{ int id;
char name[50];
float salary;
};

Now write given code inside the main() function.

struct employee e1, e2;

The variables e1 and e2 can be used to access the values stored in the
structure.
2nd way:
Let's see another way to declare variable at the time of defining the structure.

struct employee
{ int id;
char name[50];
float salary;
}e1,e2;

Which approach is good?


If number of variables are not fixed, use the 1st approach. It provides you the
flexibility to declare the structure variable many times.

3
If no. of variables are fixed, use 2nd approach. It saves your code to declare
a variable in main() function.
Accessing members of the structure
There are two ways to access structure members:
 By . (member or dot operator)
 By -> (structure pointer operator)
Let's see the code to access the id member of p1 variable by. (member)
operator:
p1.id

C Structure example: Let's see a simple example of structure in C language.

4
Let's see another example of the structure in C language to store many
employees information.

5
II. typedef in C
The typedef is a keyword used in C programming to provide some meaningful
names to the already existing variable in the C program. It behaves similarly
as we define the alias for the commands. In short, we can say that this
keyword is used to redefine the name of an already existing variable.

Syntax of typedef
typedef <existing_name> <alias_name>

In the above syntax, 'existing_name' is the name of an already existing


variable while 'alias name' is another name given to the existing variable.
For example, suppose we want to create a variable of type unsigned int, then
it becomes a tedious task if we want to declare multiple variables of this type.
To overcome the problem, we use a typedef keyword.

typedef unsigned int unit;

In the above statements, we have declared the unit variable of type unsigned
int by using a typedef keyword.

Now, we can create the variables of type unsigned int by writing the following
statement:

unit a, b;

instead of writing:

unsigned int a, b;

Till now, we have observed that the typedef keyword provides a nice shortcut
by providing an alternative name for an already existing variable. This
keyword is useful when we are dealing with the long data type especially,
structure declarations.

6
Let's understand through a simple example:

Using typedef with structures


Consider the below structure declaration:

struct student
{
char name[20];
int age;
};
struct student s1;

In the above structure declaration, we have created the variable of student


type by writing the following statement:

struct student s1;

7
The above statement shows the creation of a variable, i.e., s1, but the
statement is quite big. To avoid such a big statement, we use the typedef
keyword to create the variable of type student.

struct student
{
char name[20];
int age;
};
typedef struct student stud;
stud s1, s2;

In the above statement, we have declared the variable stud of type struct
student. Now, we can use the stud variable in a program to create the
variables of type struct student.

The above typedef can be written as:

typedef struct student


{
char name[20];
int age;
} stud;
stud s1,s2;

From the above declarations, we conclude that typedef keyword reduces the
length of the code and complexity of data types. It also helps in understanding
the program.

Let's see another example where we typedef the structure declaration.

8
III. C Array of Structures

An array of structres in C can be defined as the collection of multiple structures


variables where each variable contains information about different entities.
The array of structures in C are used to store information about multiple
entities of different data types. The array of structures is also known as the
collection of structures.

9
LAB: Create a program that create an array of structures that stores
information of 5 students and prints it.

10
Result/Output:

IV. C Nested Structure

C provides us the feature of nesting one structure within another structure by


using which, complex data types are created. For example, we may need to
store the address of an entity employee in a structure. The attribute address
may also have the subparts as street number, city, state, and pin code. Hence,
to store the address of the employee, we need to store the address of the
employee into a separate structure and nest the structure address into the
structure employee. Consider the following program.

11
Example:

The structure can be nested in the following ways.


 By separate structure
 By Embedded structure

Separate structure
Here, we create two structures, but the dependent structure should be used
inside the main structure as a member. Consider the following example.
struct Date
{
int dd;
int mm;
int yyyy;
};

12
struct Employee
{
int id;
char name[20];
struct Date doj;
}emp1;

As you can see, doj (date of joining) is the variable of type Date. Here doj is
used as a member in Employee structure. In this way, we can use Date
structure in many structures.
Embedded structure
The embedded structure enables us to declare the structure inside the
structure. Hence, it requires less line of codes but it can not be used in multiple
data structures. Consider the following example.
struct Employee
{
int id;
char name[20];
struct Date
{
int dd;
int mm;
int yyyy;
}doj;
}emp1;

Accessing Nested Structure


We can access the member of the nested structure by
Outer_Structure.Nested_Structure.member as given below:

13
e1.doj.dd
e1.doj.mm
e1.doj.yyyy

LAB: Nested Structure example:

Passing structure to function


Just like other variables, a structure can also be passed to a function. We may
pass the structure members into the function or pass the structure variable at
once. Consider the following example to pass the structure variable employee
to a function display() which is used to display the details of an employee.

14
15

You might also like