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

Structures in C

The document discusses structures in C programming. Structures allow grouping of different data types together to organize data in a structured way. The basic syntax to define a structure uses the struct keyword followed by the structure name and data members. Structures can be nested by defining one structure within another. Functions can be passed entire structures by value or address for pass by reference. An example program checks student eligibility for a scholarship by passing a structure to a function and evaluating marks.

Uploaded by

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

Structures in C

The document discusses structures in C programming. Structures allow grouping of different data types together to organize data in a structured way. The basic syntax to define a structure uses the struct keyword followed by the structure name and data members. Structures can be nested by defining one structure within another. Functions can be passed entire structures by value or address for pass by reference. An example program checks student eligibility for a scholarship by passing a structure to a function and evaluating marks.

Uploaded by

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

Structures in C

Introduction
• In C Programming, Arrays are helpful to store a group of similar data
type elements.
• But, there are some situations where we have to group non-similar
data types (int, float, char, etc.).
• To handle these type situations C programming introduced the
concept of Structures.
• Structures in C are used to group different data types to organize the
data in a structural way.
• struct keyword is used to create structures in C programming.
• For example, we are storing employee details such as name, id, age,
address, and salary.
• From the names, you can understand that they are not the same data
type.
• Normally we create a separate variable for the name, id, address,
age, and salary, but how about storing the same for 5 employees? It
will be difficult for the developer to assign the variable names to
each.
• So, We create a C structure to handle this using struct yeyword and
assign the name as an employee.
Structures in C Syntax
• The basic syntax of the C structures using struct is as shown below
struct Structure_Name
{
Data_Type Variable_Name;

Data_Type Variable_Name;
………….
};
• The struct is the system reserved keyword used to create structures
and to access structures.
• Structure_Name: Name you desire to give the structure. For ex,
employees, person, students
• Data_Type: Data type of the declared variable. For example, int, float,
char.
• Variable_Name – For example, id, name, age, salary.
struct Example

struct Employee // Structure Name = Employee


{
int age; //Data_Type = int; Variable Name = age

char name[50]; //Data Type = char Array; Variable_Name = name

float salary; //Data Type = float; Variable Name = salary


};
Declaration of Structures in C Programming
• Members inside the structures will not store any memory location
until they are associated with structure variables.
• So, we have to create the structure variable before using it.
• We can declare the C structure variables in multiple ways.
First Approach to create Structure
• Declaring the structure first and then in the main function creating
the structure variable
struct Employee
{
int age;
char name[50];
float salary;
};
//Creating the Structure variable in main() function
struct Employee emp1, emp2;
emp1 = {25, “Dave”, 25000};

NB: Emp1 and emp2 = variables of struct data type.


Second Approach to create Structure
• We are creating the structure variables at the time of the C structure
declaration.
struct Employee
{
int age;
char name[50];
float salary;
} emp1, emp2;
Accessing Structure Members
• We can locate the structure members using the dot (.) operator or
members operator.
Let us use the above struct example.
//Assigning the values
emp2.age = 26;
strcpy (emp2.name, “Michel”);
emp2.salary = 46000.50;
Example
• In this structure example program, we are going to declare the
structure with 5 data members. Next, we will display each structure
item as output.
• Example
Details of the Employee1
Employee Id = 101
Employee Age = 25
Employee Name = Dave
Employee Department = IT
Employee Salary = 25000.50

Details of the Employee2


Employee Id = 102
Employee Age = 28
Employee Name = Christofer
Employee Department = Science
Employee Salary = 32000.70
Nested Structures in C Programming
• In our previous post, we discussed Structures, and they are pretty
good to group different data type members.
• However, there are some problems, such as listing more number of
variables inside the structure and Code repetitiveness.
• For example, assume we are working with Employee, Student, and
Person data.
• So, we will need to declare 3 structures, i.e., Employee, Student, and
Person.
• Each structure may have different members (Ex: For Student
structure: Roll Number, Class Number, Marks, etc.).
• But there may be some common members such as Age, door number
or street name, city.
• In general, adding door number, street name, city members to all the
three structures means repetitive work!.
• To resolve these situations, C Programming allows us to use Nested
structures.
• Like Loops, Nested structures in C are the Structures inside the
Structure.
• Using this, we can create one structure called address and add door
number, street name, city members under it.
• Now, we can call this address structure from the Employee, Student,
and Person structures.
• We can declare the C nested structure in multiple ways:
First Approach for Nested Structures
• First, declaring the address structure, and then creating the address
variable inside the Employee structure.
• Declaring the address Structure
Here, declaring the Employee Structure
struct Employee
{
int age;
char Name[50];
char Department[20];
struct address add; // creating the address variable = add
float salary;
};
Second Approach for Nested Structures
struct Employee //// Declaring the Employee structure
{
int age;
char Name[50];
char Department[20];
float salary;
struct address // Declaring the address structure
{
int Door_Number;
char Street_Name[20];
char City[20];
int Postcode;
} add; // creating the address variable = add
};
Example
• In this C nested structures Program, We are going to declare Nested
structures (structure inside the structure).
• Then we are going to display them in two ways:
• Creating Structure Variable and assign the corresponding values to
them (Normal Variable).
• Creating Structure Pointer Variable and assign the corresponding
values to them (Pointer Variable)
• Program
Points to Remember in C Nested Structures
• To print the structure variable, we have to use the dot operator (.)
• To print the C nested structure variable, we have to use two dot
operators such as (Structure Variable. Nested Structure Variable.
Structure Member)
• printing the structure pointer variable, we have to use an arrow
operator instead of the dot operator. (Structure Variable -> Structure
Member)
• To print the nested structure variable using pointers, we have to use
a combination of the arrow operator and dot operator. (Structure
Variable -> Nested Structure Variable. Structure Member)
Structures and Functions in C
• The C Programming allows us to pass the structures as the function
parameters.
• We can pass the C structures to functions in 3 ways:
i. Passing each item of the structure as a function argument. It is similar
to passing normal values as arguments. Although it is easy to
implement, we don’t use this approach because if the size of a
structure is a bit larger, then our life becomes miserable.
ii.Pass the whole structure as a value.
iii.We can also Pass the address of the structure (pass by reference).
Pass Structure to a Function By Value
• If the structure is passed to the function by the value, then Changes
made to the structure variable members within the function will not
reflect the original structure members.
• This program for Structures and Functions in C, User is asked to enter,
Student Name, First Year Marks, and Second Year Marks.
• By using these values, this program will check whether the student is
eligible for a scholarship or not.
• Please refer Call By Value and Call By Reference to the Functions post
to know the difference between pass by value and pass by reference.
• Here, we are going to discuss the second and third approaches

You might also like