0% found this document useful (0 votes)
12 views12 pages

Structures in C

The document provides an overview of structures in C, detailing their definition, creation, and basic operations. It explains how to define structures, access their members, initialize them, and pass them to functions, as well as the use of typedef for creating aliases. Additionally, it discusses the concept of arrays of structures for storing multiple entities of the same type.

Uploaded by

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

Structures in C

The document provides an overview of structures in C, detailing their definition, creation, and basic operations. It explains how to define structures, access their members, initialize them, and pass them to functions, as well as the use of typedef for creating aliases. Additionally, it discusses the concept of arrays of structures for storing multiple entities of the same type.

Uploaded by

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

Structures in C

Presented by,
Name University Roll no
Aniruddha Das 35000324048
Soumyadeep Mondal 35000324061
Barnita Das 35000324055
Amit Das 35000324056
Trinni ghosh 35000324037
Agenda
Introduction 1
Defining structures 2
typedef for Structures 3
Array of Structures 4

Summary 5
1 Introduction
// Example
#include <stdio.h>
In C, a structure is a user- // Defining a structure
defined data type that can be struct A {
used to group items of int x;
};
possibly different types into a
single type. int main() {
The struct keyword is used to
define a structure. The items // Creating a structure
variable
in the structure are called struct A a;
its member and they can be of // Initializing member
any valid data type. a.x = 11;
printf("%d", a.x);
return 0;
}
// output = 11
2 Defining structures
There are two steps of creating a structure in C:
1. Structure Definition
2. Creating Structure Variables

Structure Definition
A structure is defined using the struct keyword
followed by the structure name and its members.
It is also called a structure template or
structure prototype, and no memory is allocated
to the structure in the declaration.
struct structure_name { structure_name: Name of the structure.
data_type1 member1; member1, member2, …: Name of the
data_type2 member2; members.
… data_type1, data_type2, …: Type of the
}; members.
Creating Structure Variable

After structure definition, we have to create variable of


that structure to use it. It is similar to the any other type
of variable declaration:

struct strcuture_name var;

We can also declare structure variables with structure definition.

struct structure_name {

}var1, var2….;
Basic Operations of Structure

1. Access Structure Members: To access or structure_name . member1;


modify members of a structure, we use the ( strcuture_name . member2;
. ) dot operator. This is applicable when we
are using structure variables directly.

In the case where we have a pointer to


structure_ptr -> member1
the structure, we can also use the arrow structure_ptr -> member2
operator to access the members.

2. Initialize Structure Members: Structure struct structure_name


members cannot be initialized with the str;
declaration. str.member1 = value1;
….
Initialization using Assignment Operator
struct structure_name str = {value1, value2,
Initialization using Initializer List : value3 ….};

Initialization using Designated Initializer List:


struct structure_name str = { .member1 = value1, .member2 = value2, .member3 = value3 };
#include <stdio.h>
3. Copy Structure: Copying #include <stdlib.h>
structure is simple as copying struct Student {
int id;
any other variables. For
char grade;
example, s1 is copied into s2 };
using assignment operator. int main() {
struct Student s1 = {1, 'A'};

// Create a copy of student s1


struct Student s1c = s1;

printf("Student 1 ID: %d\n", s1c.id);


// Output
printf("Student 1 Grade: %c", s1c.grade);
Student 1 ID: 1 return 0;
Student 1 Grade: A
}
4. Passing Structure to Functions:
Structure can be passed to a function
in the same way as normal variables. #include <stdio.h>
Though, it is recommended to pass it // Structure definition
as a pointer to avoid copying a large struct A {
amount of data. int x;
};
// Function to increment values
void increment(struct A a, struct A* b) {
a.x++;
b->x++;
}
int main() {
struct A a = { 10 };
struct A b = { 10 };
// Passing a by value and b by pointer
increment(a, &b);
// Output printf("a.x: %d \tb.x: %d", a.x, b.x);
a.x: 10 return 0;
b.x: 11 }
3 typedef for Structures
The typedef keyword #include <stdio.h>
is used to define an // Defining structure
alias for the already typedef struct {
int a;
existing datatype. In
} str1;
structures, we have to // Another way of using typedef with
use the struct keyword structures
along with the typedef struct {
structure name to int x;
define the variables. } str2;
Sometimes, this int main() {
increases the length // Creating structure variables using
new names
and complexity of the
str1 var1 = { 20 };
code. We can use the str2 var2 = { 314 };
typedef to define some printf("var1.a = %d\t", var1.a);
new shorter name for printf("var2.x = %d\n", var2.x);
the structure. return 0;
} //output = var1.a = 20 var2.x = 314
4 Array of Structures
#include <stdio.h>
An array of structures is
#include <string.h>
simply an array where each
element is a structure. It // Structure definition
allows you to store several struct A {
structures of the same type in int var;
a single array. };
int main() {
Explanation: We define a
Person structure with name // Declare an array of structures
and age as members. An array struct A arr[2];
of 2 Person structures is arr[0].var = 10;
declared, and each element is arr[1].var = 20;
populated with different
people’s details. A for loop is for (int i = 0; i < 2; i++)
used to iterate through the printf("%d\t", arr[i].var);
array and print each person's return 0;
} // Output = 10 20
information.
5 Summary

There are conditions where we need to keep more


than one attribute for an entity in the C language. An
entity need not include all the information of a single
type. It is capable of having various properties from
various data types. As an illustration, a student object
may have a name (string), roll number (int), and marks
(float). We have the following methods for storing this
sort of student-related information:
• Construct individual arrays for storing names, roll
numbers, and marks.
• Use a special data structure to store the collection
of different data types.
Thank you

You might also like