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

Chapter 2 Structure Part 2

The document outlines a course on Problem Solving with Programming, focusing on structures and functions in C programming. It includes course objectives, outcomes, evaluation schemes, and methods for passing structures to functions. Additionally, it provides examples and references for further learning on the topic.

Uploaded by

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

Chapter 2 Structure Part 2

The document outlines a course on Problem Solving with Programming, focusing on structures and functions in C programming. It includes course objectives, outcomes, evaluation schemes, and methods for passing structures to functions. Additionally, it provides examples and references for further learning on the topic.

Uploaded by

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

INSTITUTE - UIE

DEPARTMENT- ACADEMIC UNIT-2

Bachelor of Engineering (Computer Science & Engineering)


Subject Name: Problem solving with programming

Code:20CST111
Unit-3

Structure: Structure and functions, Structures DISCOVER . LEARN . EMPOWER


and pointers
Problem solving
with
programming
Course Objectives
The course aims to provide exposure to problem-
solving through programming.
The course aims to raise the programming skills
of students via logic building capability.
With knowledge of C programming language,
students would be able to model real world
problems.
2
Course Outcomes
CO Title Level
Number

CO1 Identify​ situations where Understand


computational methods would
be useful.
CO2 Approach the programming tasks Remember
using techniques learnt and
write pseudo-code.
CO3 Choose the right data Understand
representation formats based on
the requirements of the
problem.
CO4 Use the comparisons and Understand
limitations of the various
programming constructs and
choose the right one for the task.
3
Scheme of Evaluation
Sr. Type of Assessment Weightage of actual Frequency of Task Final Weightage in Internal Remarks
No. Task conduct Assessment (Prorated
Marks)

1. Assignment* 10 marks of One Per Unit 10 marks As applicable to


each assignment course types depicted
above.
2. Time Bound 12 marks for each One per Unit 4 marks As applicable to
Surprise test course types
Test depicted above.
3. Quiz 4 marks of each quiz 2 per Unit 4marks As applicable to
course types
depicted above.
4. Mid-Semester Test** 20 marks for one 2 per semester 20 marks As applicable to
MST. course types
depicted above.
5. Presentation*** Non Graded: Engagement Only for Self Study
Task MNGCourses.

6. Homework NA One per lecture topic Non-Graded: Engagement As applicable to


(of 2 Task course types
questions) depicted above.
7. Discussion Forum NA One per Non Graded: Engagement As applicable to
Chapter Task course types depicted
above.
8. Attendance and NA NA 2 marks
Engagement Score
on BB
4
Structures and Functions
• A structure can be passed to any function from main function or from
any sub function.

• Structure definition will be available within the function only.

• It won’t be available to other functions unless it is passed to those


functions by value or by address(reference).

• Else, we have to declare structure variable as global variable. That


means, structure variable should be declared outside the main
function. So, this structure will be visible to all the functions in a C
program.
5
PASSING ENTIRE STRUCTURE TO FUNCTION IN C

It can be done in below 3 ways.

• Passing structure to a function by value

• Passing structure to a function by address(reference)

• No need to pass a structure – Declare structure variable as global

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

• We can pass individual members to a function just like ordinary variables.


• The following program demonstrates how to pass structure members as arguments to the function.
• Example:
#include<stdio.h>
/*
structure is defined above all functions so it is global.
*/
struct student
{
char name[20];
int roll_no;
int marks;
};
void print_struct(char name[], int roll_no, int marks);

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

A structure can be passed to It won’t be available to other


Structure definition will be
any function from main functions unless it is passed to
available within the function
function or from any sub those functions by value or by
only.
function. address

Else, we have to declare


structure variable as global
We can also initialize the array
variable. That means, structure To access members of a
of structures using the same
variable should be declared structure using pointers, we use
syntax as that for initializing
outside the main function. So, the -> operator.
arrays.
this structure will be visible to
all the functions in a C program.

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?

2. Program to add two distances in inch-feet system(Passing structure).

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]

You might also like