Week 08 Tutorial Questions
-
Assignment 2 has been released - CS Beats.
Where can you find the important information you need to know before starting the assignment?
-
What is a struct?
How does it differ from an array?
-
How could we represent a student and their assignment 1 mark as a struct? What fields might we want to include?
-
Create a
struct student
with the name "Frankie", zid of 5151515, assignment 1 mark of 60.2 in the main function (hint, you do not need functions ormalloc
). -
Use a function to create a
struct student *
with the name "Chicken", zid of 5252525, assignment 1 mark of 71 using a function (hint, you will need to usemalloc
) -
Which of these structs is currently causing a memory leak and why?
Use
dcc --leakcheck
to confirm this. -
We have a
struct student
and astruct student *
:If we want to access the fields of these, when do we use
.
and when do we use->
? -
Edit the existing
struct student
, to be able to point to anotherstruct student
.Update your function which creates an instance of a student to account for this new pointer.
-
Build a linked list to represent a class of students by adding new students at the head of the list.
-
Create another struct definition and instance to represent the class.
It should contain the name (i.e. M13A), the number of students and a pointer to the linked list you just made.
-
Give a struct that might hold lab, assignmment and exam marks for a COMP1511 student. Remember, there is more than 1 lab and more than 1 assignment.
-
What is wrong with this program? Fix these issues in the file below.
#include <stdio.h> #define MAX_PLATE 10000 struct parking_fine { double amount; char number_plate[MAX_PLATE]; }; int read_parking_fine(struct parking_fine *fine); int main(void) { struct parking_fine f; if (read_parking_fine(f)) { printf("%lf %s\n", f.amount, f.number_plate); } return 0; } // return 1 if a parking fine was successfully read, 0 otherwise int read_parking_fine(struct parking_fine *fine) { if (scanf("%lf", (fine.amount)) != 1) { return 0; } return fgets(fine->number_plate, MAX_PLATE, stdin) != NULL; }
Assignment 2 - CS Beats
Structs
Struct Pointers
Revision questions
The remaining tutorial questions are primarily intended for revision - either this week or later in session.
Your tutor may still choose to cover some of the questions time permitting.