Chapter 2 Structure Part 2
Chapter 2 Structure Part 2
Code:20CST111
Unit-3
6
Passing Structure to Function by Value
• It means the whole structure is passed to another function with all
members and their values.
• So, this structure can be accessed from called function.
• This concept is very useful while writing very big programs in C.
• Example:
7
8
Passing Structure to Function by Address
(reference)
• It means only the address of the structure is passed to another
function.
• The whole structure is not passed to another function with all
members and their values.
• So, this structure can be accessed from called function by its address.
• Example:
9
10
void func(struct student *record);
int main()
{
Output:
struct student record;
Id is: 1
record.id=1; Name is: Raju
strcpy(record.name, "Raju"); Percentage is: 86.500000
record.percentage = 86.5;
func(&record);
return 0;
}
void func(struct student *record)
{
printf(" Id is: %d \n", record->id);
printf(" Name is: %s \n", record->name);
printf(" Percentage is: %f \n", record->percentage);
}
11
Declare structure variable as global
• Structure variables also can be declared as global variables as we
declare other variables in C.
• So, When a structure variable is declared as global, then it is visible to
all the functions in a program.
• In this scenario, we don’t need to pass the structure to any function
separately.
• Example:
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[20];
float percentage;
};
12
struct student record; // Global declaration of structure
void structure_demo();
int main()
Output:
{
Id is: 1
record.id=1; Name is: Raju
strcpy(record.name, "Raju"); Percentage is: 86.500000
record.percentage = 86.5;
structure_demo();
return 0;
}
void structure_demo()
{
printf(" Id is: %d \n", record.id);
printf(" Name is: %s \n", record.name);
printf(" Percentage is: %f \n", record.percentage);
}
13
Passing individual structure members
14
int main()
{
struct student stu = {"Tim", 1, 78};
Output:
print_struct(stu.name, stu.roll_no, stu.marks); Name: Tim
return 0; Roll no: 1
Marks: 78
}
void print_struct(char name[], int roll_no, int marks)
{
printf("Name: %s\n", name);
printf("Roll no: %d\n", roll_no);
printf("Marks: %d\n", marks);
printf("\n");
}
15
Structure and Pointers
• Pointer to structure: Pointer which stores address of structure is called as “Pointer to
Structure “.
• Explanation :
1. sptr is pointer to structure address.
2. -> and (*). both represents the same.
3. These operators are used to access data member of structure by using structure’s
pointer.
16
17
18
• Example: Output:
#include<stdio.h>
struct team {
char *name;
int members;
char captain[20];
}
t1 = {"India",11,"Dhoni"} , *sptr = &t1;
int main()
{
printf("\nTeam : %s",(*sptr).name);
printf("\nMemebers : %d",sptr->members);
printf("\nCaptain : %s",(*sptr).captain);
return 0;
}
19
Summary
20
Frequently Asked Questions
Q1 Write a Program to enter the employee Information & display the data passing Structures By Using
Pointers
Expected Output:
Enter the name of the Employee : John
Enter the Employee Id : 16
Enter Experience of the Employee : 3
---------Details List---------
Employee Name : ram
Employee Id : 16
Employee Experience : 3
Solution:
21
22
Assessment Questions:
1. Write a Program to demonstrate How to return multiple values from a function in C?
23
Discussion forum
PROBLEM:
Write a Program to add two complex numbers
Using (Passing struct by reference).
24
REFERENCES
Reference Books
1. Programming in C by Reema Thareja.
2. Programming in ANSI C by E. Balaguruswamy, Tata McGraw Hill.
3. Programming with C (Schaum's Outline Series) by Byron Gottfried Jitender
Chhabra, Tata McGraw Hill.
4. The C Programming Language by Brian W. Kernighan, Dennis Ritchie, Pearson
education.
Websites:
1. https://fresh2refresh.com/c-programming/c-passing-struct-to-function/
2. https://www.geeksforgeeks.org/how-to-pass-or-return-a-structure-to-from-a-
function-in-c-c/
3. https://www.studytonight.com/c/pointers-to-structure-in-c.php
4. https://dev.to/mikkel250/structures-and-pointers-in-c-n6i
5. https://www.programiz.com/c-programming/c-structures-pointers
YouTube Links:
1. https://www.youtube.com/watch?v=4HC8X966Q5M
2. https://www.youtube.com/watch?v=VF40lxbXR_0
25
THANK YOU
For queries
Email: [email protected]