CC-112L PF Lab-11
CC-112L PF Lab-11
CC-112L PF Lab-11
Programming Fundamentals
Laboratory 11
Structures
Version: 1.0.0
Contents:
Learning Objectives
Required Resources
General Instructions
Background and Overview
o Structure Definitions
o Initializing Structures
o Accessing Structure member with “.” and “->”
o Using Structures with Functions
o Typedef
o Unions
Activities
o Pre-Lab Activity
Define Structures
Syntax of struct
Create struct Variables
Example Code of struct
Access Members of a Structure
Task 01: Movie Data Struct
Task 02: Update Movie Data Struct
o In-Lab Activity
Why structs in C?
Keyword typedef
Typedef struct in C Example
Nested Structures
Example Code
C Structure and Function
Passing structs to functions
Return structs from a function
Passing struct by reference
Task 01: Score Board
Task 02: Student Details
o Post-Lab Activity
Unions
How to define a union?
Create union variables
Access members of a union
Difference between unions and structures
Why this difference in the size of union and structure variables?
Task 01: Accessing struct members
Submissions
Evaluations Metric
References and Additional Material
Lab Time and Activity Simulation Log
Learning Objectives:
Introduction
Structure Definitions
Initializing Structures
Accessing Structure member with “.” and “->”
Using Structures with Functions
typedef
Unions
Resources Required:
Desktop Computer or Laptop
Microsoft ® Visual Studio 2022
General Instructions:
In this Lab, you are NOT allowed to discuss your solution with your colleagues, even not
allowed to ask how is s/he doing, this may result in negative marking. You can ONLY discuss
with your Teaching Assistants (TAs) or Lab Instructor.
Your TAs will be available in the Lab for your help. Alternatively, you can send your queries
via email to one of the followings.
Teachers:
Course Instructor Prof. Dr. Syed Waqar ul Qounain swjaffry@pucit.edu.pk
Structures are derived data types; they’re constructed using objects of other types. The keyword struct
introduces a structure definition, as in
struct Person {
const char *name;
int age;
};
Initializing Structures:
Like arrays, you can initialize a struct variable via an initializer list. For example, the following
statement creates variable person1 using type struct Person and initializes member name to "Bob" and
member age to 20:
struct Person person1 = {"Bob", 1};
If there are fewer initializers than members, the remaining members are automatically initialized to 0
or NULL (for pointer members).
Typedef:
The keyword typedef enables you to create synonyms (or aliases) for previously defined types. It’s
commonly used to create shorter names for struct types and simplify declarations of types like function
pointers. For example, the following typedef defines person as a synonym for type struct Person
typedef struct Person person;
Unions:
Like a structure, a union is a derived data type, but its members share the same memory. At different
times during program execution, some variables may not be relevant when others are. So, a union shares
the space rather than wasting storage on variables that are not in use. A union’s members can be of any
type. The number of bytes used to store a union must be at least enough to hold its largest member.
Activities:
Pre-Lab Activities:
Define Structures:
In C programming, a struct (or structure) is a collection of variables (can be of different types) under a
single name. Before you can create structure variables, you need to define its data type. To define a
struct, the struct keyword is used.
Syntax of struct:
struct structureName {
dataType member1;
dataType member2;
…
};
For example:
Example C struct:
In this program, we have created a struct named Person. We have also created a variable of Person
named person1.
In main (), we have assigned values to the variables defined in Person for the person1 object.
This is because name is a char array (C-string) and we cannot use the assignment operator = with it
after we have declared the string.
Title
Director
Year Released
Running Time (in minutes)
The program should create two MovieData variables, store values in their members, and pass each one,
in turn, to a function that displays the information about the movie in a clearly formatted manner.
Sample Output:
Sample Output:
In-Lab Activities:
Why structs in C?
Suppose you want to store information about a person: his/her name, citizenship number, and salary.
You can create different variables name, age and salary to store this information.
What if you need to store information of more than one person? Now, you need to create different
variables for each information per person: name1, cage1, salary1, name2, age2, salary2, etc.
A better approach would be to have a collection of all related information under a single name Person
structure and use it for every person.
Keyword typedef:
We use the typedef keyword to create an alias name for data types. It is commonly used with structures
to simplify the syntax of declaring variables.
For example, let us look at the following code:
Example Code:
Output:
Example Code:
Output:
Here, a struct variable s1 of type struct student is created. The variable is passed to the display ()
function using display(s1); statement.
Here, the getInformation () function is called using s = getInformation (); statement. The function
returns a structure of type struct student. The returned structure is displayed from the main () function.
Notice that, the return type of getInformation () is also struct student.
Example Code:
Output:
In the above program, three structure variables c1, c2 and the address of result is passed to the
addNumbers () function. Here, result is passed by reference.
When the result variable inside the addNumbers () is altered, the result variable inside the main ()
function is also altered accordingly.
Player’s Name
Player’s Number
Points Scored by Player
To store all players of the team, the program should keep an array of 12 of these structures. Each
element is for a different player on a team. Write a function that populates the array of structures in a
function by user given data for each player. It should then show a table that lists each player’s
number, name, and points scored. The program should also calculate and display the total points
earned by the team. The number and name of the player who has earned the most points should also
be displayed.
To complete this task, you have to implement following functions:
PopulateArray: to populate the array of structures by user provided data.
DisplayScoreBoard: to display the name, number and score of each player.
FindTopScorer: this function should return the player with highest score.
Call each of these functions in main () to demonstrate your work.
Input Validation: Do not accept negative values for player’s numbers or points scored.
Fig. 16 (In-Lab Task 01)
Submit “.c” file named your “ScoreBoard.c” on Google Classroom.
Name
rollNo
Date of birth
Number of courses the student is studying
Marks of all courses the student is studying
CGPA
Date of birth of the student should be of type DATE, therefore you should implement another structure
to represent the date of birth. Marks of student should be stored in an array where size of array should
be given by the user. Create 3 variables of type Student in the main () function. Identify any student
who is going to dropout due to low CGPA (consider the bare minimum CGPA to continue is 2.5).
Minimum marks to pass a course are 50. Display all the failed courses for each student.
Input Validation: Do not accept negative values and values greater than 100 for marks of each course.
Do not accept CGPA greater than 4.00 and less than 0.00.
Submit “.c” file named your “Student.c” on Google Classroom.
Post-Lab Activities:
Unions:
A union is a user-defined type similar to structs in C except for one key difference.
Structures allocate enough space to store all their members, whereas unions can only hold one member
value at a time.
However, the size of uJob is 32 bytes. It's because the size of a union variable will always be the size
of its largest element. In the above example, the size of its largest element, (name [32]), is 32 bytes.
With a union, all members share the same memory.
Submissions:
For In-Lab Activity:
Save the files on your PC.
TA’s will evaluate the tasks offline.
For Pre-Lab & Post-Lab Activity:
Submit the .c file on Google Classroom and name it to your roll no.
Evaluations Metric:
All the lab tasks will be evaluated offline by TA’s
Division of Pre-Lab marks: [50 marks]
Task 01: Movie Data Struct [30 marks]
Task 02: Update Movie Data Struct [20 marks]
Division of In-Lab marks: [60 marks]
Task 01: Score Board [30 marks]
Task 02: Student Details [30 marks]
Division of Post-Lab marks: [30 marks]
Task 01: Accessing struct members [30 marks]
Unions
https://www.programiz.com/c-programming/c-unions