0% found this document useful (0 votes)
2 views45 pages

Module 1.2 - Structure

The document provides an overview of structures in C programming, detailing their definition, declaration, initialization, and usage. It covers nested structures, arrays of structures, and how to access and manipulate structure members, including through functions. Additionally, it explains the use of typedef for creating new data types and includes examples for clarity.
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)
2 views45 pages

Module 1.2 - Structure

The document provides an overview of structures in C programming, detailing their definition, declaration, initialization, and usage. It covers nested structures, arrays of structures, and how to access and manipulate structure members, including through functions. Additionally, it explains the use of typedef for creating new data types and includes examples for clarity.
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

Structures

Introduction, Nested Structures,


Arrays of Structures, Structures and
Functions, Self-Referential
Structures

Amruthasree V M, Assistant Professor, NIE


Structures
• Structure is basically a user-defined data type
that can store related information about an entity
(even of different data types) together.
• The major difference between a structure and an
array is that an array can store only information of
same data type.
• A structure is a collection of variables under a
single name. The variables within a structure
are of different data types and each has a name
that is used to select it from the structure.
Amruthasree V M, Assistant Professor, NIE
Structure Declaration
• A structure is declared using the keyword struct
followed by the structure name.
• All the variables of the structure are declared
within the structure.
struct struct–name
{
data_type var–name;
data_type var–name;
...............
};
Amruthasree V M, Assistant Professor, NIE
Example:- related information for a student probably
would be: roll_number, name, course, and fees.

This structure can be declared as:

struct student
{
int r_no;
char name[20];
char course[20];
float fees;
};

Amruthasree V M, Assistant Professor, NIE


• Each variable name declared within a structure
is called a member of the structure.
• The structure declaration, however, does not
allocate any memory or consume storage
space.
• It just gives a template that conveys to the C
compiler how the structure would be laid out
in the memory and also gives the details of
member names.
• Memory is allocated for the structure when we
declare a variable of the structure.
Amruthasree V M, Assistant Professor, NIE
• We can define a structure variable of student by writing:
struct student stud1;
• Here, struct student is a data type and stud1 is a variable.
• Another way of declaring structure variables. (The variables
are declared at the time of structure declaration.)

struct student
{
int r_no;
char name[20];
char course[20];
float fees;
} stud1, stud2;
Amruthasree V M, Assistant Professor, NIE
• If you want to declare more than one variable of
the structure, then separate the variables using a
comma.
• When we declare variables of the structure,
separate memory is allocated for each variable.

Amruthasree V M, Assistant Professor, NIE


Typedef Declarations
• The typedef keyword enables the programmer to create
a new data type name by using an existing data type.

• By using typedef, no new data is created, rather an


alternate name is given to a known data type.

• The general syntax:

typedef existing_data_type new_data_type;

• Note that typedef statement does not occupy any


memory; it simplyAmruthasree
defines a new type.
V M, Assistant Professor, NIE
• For example, if we write
typedef int INTEGER;
then INTEGER is the new name of data type int.
• To declare variables using the new data type
name, precede the variable name with the new
data type name.
INTEGER num=5;

Amruthasree V M, Assistant Professor, NIE


typedef struct student
{
int r_no;
char name[20];
char course[20];
float fees;
}stu;
• You have preceded the structure’s name with the
typedef keyword, student becomes a new data type.
• Therefore, now you can declare the variables of this
new data type.
• To declare a variable of structure student, you may
write
stu s1;
Note that we have not written struct student stud1.
Amruthasree V M, Assistant Professor, NIE
typedef struct
{
char first_name[20];
char mid_name[20];
char last_name[20];
}NAME;

typedef struct naam


{
char first_name[20];
char mid_name[20];
char last_name[20];
}NAME;
Amruthasree V M, Assistant Professor, NIE
Initialization of Structures
• Initializing a structure means assigning some
constants to the members of the structure.
• When the user does not explicitly initialize the
structure, then C automatically does it.
• For int and float members, the values are
initialized to zero, and char and string members
are initialized to '\0' by default.
• The initializers are enclosed in braces and are
separated by commas.
Amruthasree V M, Assistant Professor, NIE
• The general syntax to initialize a structure variable is as follows:
struct struct_name
{
data_type member_name1;
data_type member_name2;
data_type member_name3;
.......................
}struct_var = {constant1, constant2, constant3,...};

Or

struct struct_name
{
data_type member_name1;
data_type member_name2;
data_type member_name3;
.......................
};
struct struct_name struct_var = {constant1, constant2, constant 3,...};
Amruthasree V M, Assistant Professor, NIE
• For example,

struct student
{
int r_no;
char name[20];
char course[20];
float fees;
}stud1 = {01, "Rahul", "BCA", 45000};
Or,
struct student stud1 = {01, "Rahul", "BCA", 45000};
Amruthasree V M, Assistant Professor, NIE
• When all the members of a structure are not
initialized, it is called partial initialization.
• In case of partial initialization, first few members
of the structure are initialized and those that are
uninitialized are assigned default values.

Amruthasree V M, Assistant Professor, NIE


Accessing the Members of a Structure
• A structure member variable is generally accessed
using a '.' (dot) operator.
• The syntax
struct_var.member_name
• The dot operator is used to select a particular
member of the structure.
• For example, to assign values to the individual data
members of the structure variable studl, we may
write
stud1.r_no = 01;
[Link] = "Rahul";
[Link] = "BCA";
[Link] = 45000;
Amruthasree V M, Assistant Professor, NIE
• To input values for data members of the structure
variable stud1, we may write
scanf("%d", &stud1.r_no);
scanf("%s", [Link]);
• Similarly, to print the values of structure variable
stud1, we may write
printf("%s", [Link]);
printf("%f", [Link]);

Amruthasree V M, Assistant Professor, NIE


Copying and Comparing Structures
• We can assign a structure to another structure of the
same type.
• For example, if we have two structure variables stud1 and
stud2 of type struct student given as

struct student stud1 = {01, "Rahul", "BCA", 45000};


struct student stud2;

• Then to assign one structure variable to another,


stud2 = stud1;

• This statement initializes the members of stud2 with the


values of members of stud1.
Amruthasree V M, Assistant Professor, NIE
Amruthasree V M, Assistant Professor, NIE
• C does not permit comparison of one
structure variable with another.
• However, individual members of one structure
can be compared with individual members of
another structure.
• For example, to compare the fees of two
students, we will write
• if([Link] > [Link]) //to check if fees of
stud1 is greater than stud2

Amruthasree V M, Assistant Professor, NIE


Write a program using structures to read and display the
information about a student.
#include<stdio.h>
printf("\n **STUDENT'S DETAILS **");
int main()
printf("\n ROLL No. = %d", stud1.roll_no);
{
printf("\n NAME = %s", [Link]);
struct student
printf("\n FEES = %f", [Link]);
{
printf("\n DOB = %s", [Link]);
int roll_no;
return 0;
char name[80];
}
float fees;
Output
char DOB[80];
Enter the roll number : 01
};
Enter the name : Rahul
struct student stud1;
Enter the fees : 45000
printf("\n Enter the roll number : ");
Enter the DOB : 25–09–1991
scanf("%d", &stud1.roll_no);
*****STUDENT’S DETAILS *****
printf("\n Enter the name : ");
ROLL No. = 01
scanf("%s", [Link]);
NAME = Rahul
printf("\n Enter the fees : ");
FEES = 45000.00
scanf("%f", &[Link]);
DOB = 25–09–1991
printf("\n Enter the DOB : ");
scanf("%s", [Link]); Amruthasree V M, Assistant Professor, NIE
NESTED STRUCTURES
• A structure can be placed within another structure.
• A structure that contains another structure as its
member is called a nested structure.

Amruthasree V M, Assistant Professor, NIE


typedef struct naam
{
char first_name[20]; • Assigning values to the structure fields,
char mid_name[20];
char last_name[20];
student stud1;
}NAME;
typedef struct date stud1.r_no = 01;
{ [Link].first_name = "Janak";
int dd; [Link].mid_name = "Raj";
int mm; [Link].last_name = "Thareja";
int yy; [Link] = "BCA";
}DATE; [Link] = 15;
[Link] = 09;
typedef struct STUDENT [Link] = 1990;
{ [Link] = 45000;
int r_no;
NAME name;
char course[20];
DATE DOB;
float fees;
} student; Amruthasree V M, Assistant Professor, NIE
Write a program to read and display the information of a
student using a nested structure.
#include <stdio.h> printf("\n Enter the roll number : ");
int main() scanf("%d", &stud1.roll_no);
{ printf("\n Enter the name : ");
struct DOB scanf("%s", [Link]);
{ printf("\n Enter the fees : ");
int day; scanf("%f", &[Link]);
int month;
int year; printf("\n Enter the DOB : ");
}; scanf("%d %d %d", &[Link],
struct student &[Link], &[Link]);
{ printf("\n *****STUDENT'S DETAILS *****");
int roll_no; printf("\n ROLL No. = %d", stud1.roll_no);
char name[100]; printf("\n NAME = %s", [Link]);
float fees; printf("\n FEES = %f", [Link]);
struct DOB date;
}; printf("\n DOB = %d – %d – %d", [Link],
struct student stud1; [Link], [Link]);
}
Amruthasree V M, Assistant Professor, NIE
ARRAYS OF STRUCTURES
The general syntax for declaring an array of structures can be
given as,

struct struct_name
{ Example:
data_type member_name1; struct student
data_type member_name2; {
data_type member_name3;
....................... int r_no;
}; char name[20];
struct struct_name struct_var[index]; char course[20];
float fees;
};
struct student stud[30];
Amruthasree V M, Assistant Professor, NIE
• To assign values to the ith student of the class, we will
write
stud[i].r_no = 09;
stud[i].name = "RASHI";
stud[i].course = "MCA";
stud[i].fees = 60000;

• In order to initialize the array of structure variables


at the time of declaration,
struct student stud[3] = {{01, "Aman", "BCA", 45000},
{02, "Aryan", "BCA", 60000},
{03,"John", "BCA", 45000}};
Amruthasree V M, Assistant Professor, NIE
Write a program to read and display the information of
all the students in a class and display the entire
information
for(i=0;i<n;i++)
#include <stdio.h> {
int main() printf("\n Enter the roll number : ");
{ scanf("%d", &stud[i].roll_no);
printf("\n Enter the name : ");
struct student scanf(“%s”,stud[i].name);
{ printf("\n Enter the fees : ");
int roll_no; scanf("%d",&stud[i].fees);
printf("\n Enter the DOB : ");
char name[80]; scanf(“%s”,stud[i].DOB);
int fees; }
char DOB[80];
for(i=0;i<n;i++)
}; {
struct student stud[50]; printf(“\DETAILS OF STUDENT %d",
i+1);
int n, i; printf("ROLL No. = %d", stud[i].roll_no);
printf("\n Enter the number of students : "); printf("\n NAME = %s", stud[i].name);
scanf("%d", &n); printf("\n FEES = %d", stud[i].fees);
printf("\n DOB = %s", stud[i].DOB);
Amruthasree V M, Assistant Professor, NIE
}
STRUCTURES AND FUNCTIONS
• A function may access the members of a
structure in three ways.

Amruthasree V M, Assistant Professor, NIE


1. Passing Individual Members
• To pass any individual member of a structure to
a function, we must use the direct selection
operator to refer to the individual members.
• The called program does not know if a variable
is an ordinary variable or a structured member.

Amruthasree V M, Assistant Professor, NIE


#include <stdio.h>
struct POINT
{
int x;
int y;
};

void display(int, int);

int main()
{
struct POINT p1 = {2, 3};
display(p1.x, p1.y);
return 0;
}

void display(int a, int b)


{
printf(" The coordinates of the point are: %d %d", a, b);
}
Amruthasree V M, Assistant Professor, NIE
2. Passing the Entire Structure
• When a structure is passed as an argument, it is
passed using the call by value method, i.e., a
copy of each member of the structure is made.
• The general syntax for passing a structure to a
function and returning a structure can be given as,

Amruthasree V M, Assistant Professor, NIE


#include <stdio.h>
struct POINT
{
int x;
int y;
};

void display(struct POINT);

int main()
{
struct POINT p1 = {2, 3};
display(p1);
return 0;
}

void display(struct POINT p)


{
printf("The coordinates of the point are: %d %d", p.x, p.y);
} Amruthasree V M, Assistant Professor, NIE
3. Passing Structures through
Pointers
• Passing large structures to functions using the call by value
method is very inefficient.
• Therefore, it is preferred to pass structures through pointers.
• The syntax to declare a pointer to a structure

struct struct_name
{
data_type member_name1;
data_type member_name2;
data_type member_name3;
.......................
}*ptr;
or,
struct struct_name *ptr; Amruthasree V M, Assistant Professor, NIE
• For our student structure, we can declare a pointer variable by writing
struct student *ptr_stud, stud;

• The next thing to do is to assign the address of stud to the pointer


using the address operator (&).
ptr_stud = &stud;

• To access the members of a structure, we can write


(*ptr_stud).roll_no;

• C introduces a new operator to do the same task. This operator is


known as ‘pointing-to’ operator (->). It can be used as:
ptr_stud -> roll_no = 01;

• This statement is far easier than its alternative.


Amruthasree V M, Assistant Professor, NIE
Write a program to initialize the members of a structure by
using a pointer to the structure.
printf("\n Enter the Course = ");
#include <stdio.h>
scanf("%s",ptr_stud1 -> course);
struct student
{
int r_no; printf("\n Enter the Fees = ");
char name[20]; scanf("%d", &ptr_stud1 -> fees);
char course[20];
int fees;
printf("\n DETAILS OF THE STUDENT");
};
int main()
{ printf("\n ROLL NUMBER = %d", ptr_stud1 –> r_no);
struct student stud1, *ptr_stud1; printf("\n NAME = %s", ptr_stud1 –> name);
ptr_stud1 = &stud1; printf("\n COURSE = %s", ptr_stud1 –> course);
printf("Enter the details of the student :"); printf("\n FEES = %d", ptr_stud1 –> fees);
printf("\n Enter the Roll Number =");
scanf("%d", &ptr_stud1 -> r_no); return 0;
}
printf("\n Enter the Name = “);
scanf("%s",ptr_stud1 -> name);
Amruthasree V M, Assistant Professor, NIE
Write a program, using an array of pointers to a structure,
to read and display the data of students.
printf("\n ROLL NO.: ");
#include <stdio.h> scanf("%d", &ptr_stud[i]–>r_no);
#include <alloc.h>
struct student printf("\n NAME: ");
{ scanf("%s", ptr_stud[i]–>name);
int r_no; printf("\n COURSE: ");
char name[20]; scanf("%s", ptr_stud[i]–>course);
char course[20];
printf("\n FEES: ");
int fees; scanf("%d", &ptr_stud[i]–>fees);
}; }
struct student *ptr_stud[10]; printf("\n DETAILS OF STUDENTS");
for(i=0;i<n;i++)
int main() {
{ printf("\n ROLL NO. = %d", ptr_stud[i]–>r_no);
int i, n; printf("\n NAME = %s", ptr_stud[i]–>name);
printf("\n Enter the number of students : "); printf("\n COURSE = %s", ptr_stud[i]–>course);
printf("\n FEES = %d", ptr_stud[i]–>fees);
scanf("%d", &n); }
for(i=0;i<n;i++) return 0;
}
{
ptr_stud[i] = (struct student *)malloc(sizeof(struct student));
Write a program that printf("\n ROLL NO.: ");
scanf("%d", &ptr–>r_no);
passes a pointer to a printf("\n NAME: ");
structure to a function. scanf("%s", ptr–>name);
#include <stdio.h>
printf("\n COURSE: ");
#include <alloc.h> scanf("%s", ptr–>course);
printf("\n FEES: ");
struct student scanf("%d", &ptr–>fees);
{ display(ptr);
int r_no; return 0;
char name[20]; }
char course[20]; void display(struct student *ptr)
int fees; {
}; printf("\n DETAILS OF STUDENT");
printf("\n ROLL NO. = %d", ptr–>r_no);
void display (struct student *); printf("\n NAME = %s", ptr–>name);
printf("\n COURSE = %s ", ptr–>course);
int main() printf("\n FEES = %d", ptr–>fees);
{ }
struct student *ptr;

ptr = (struct student *)malloc(sizeof(struct student));


printf("\n Enter the data for the student ");
Static Memory Allocation
• When the allocation of memory performs at the compile time,
then it is known as static memory. In this, the memory is
allocated for variables by the compiler.
• While executing a program, the memory cannot be
changed.
• Static memory allocation is less efficient as compared to
Dynamic memory allocation.
• Once the memory is allotted, it will remain from the
beginning to end of the program.
• Static memory is deallocated automatically when the
program terminates, as the memory is reserved for the entire
duration of the program.
Amruthasree V M, Assistant Professor, NIE
Dynamic Memory Allocation
• When the memory allocation is done at the
execution or run time, then it is called dynamic
memory allocation.
• In dynamic memory allocation, while executing a
program, the memory can be changed.
• Dynamic memory allocation is preferred in the
linked list.
• Here, the memory can be alloted at any time in the
program.
• Dynamic memory allocation requires explicit
deallocation using the free() function to release the
memory back to the system when it is no longer
needed.
Amruthasree V M, Assistant Professor, NIE
malloc & calloc
• The function malloc() is used to allocate a
single block of the memory that has been
requested.
Syntax: ptr=(data_type*)malloc(byte_size)

• This command is used to assign multiple


blocks of the requested memory.

Syntax: ptr=(data_type*)calloc(number, byte_size)

Amruthasree V M, Assistant Professor, NIE


When to use Static Memory Allocation:
• Static memory allocation is best suited for
situations where the size of the data structure is
fixed and known in advance.

• It is also useful for global variables and


variables that need to be accessed frequently,
such as counters or flags.

Amruthasree V M, Assistant Professor, NIE


When to use Dynamic Memory Allocation:

• Dynamic memory allocation is best suited for


situations where the size of the data structure
is not known in advance and needs to be
changed during program execution.

• It is also useful for situations where memory


needs to be allocated and deallocated
frequently.

Amruthasree V M, Assistant Professor, NIE


SELF-REFERENTIAL STRUCTURES

• Self-referential structures are those structures


that contain a reference to the data of its same
type.
• That is, a self-referential structure, in addition to
other data, contains a pointer to a data that is of
the same type as that of the structure.
• Actually, self-referential structure is the
foundation of other data structures.

Amruthasree V M, Assistant Professor, NIE


• For example, consider the structure node given
below.
struct node
{
int val;
struct node *next;
};
• Here, the structure node will contain two types
of data: an integer value and a pointer next.
• The pointer next will point to next data that
belongs to the same structure.
Amruthasree V M, Assistant Professor, NIE
Amruthasree V M, Assistant Professor, NIE

You might also like