Week 09 Tutorial Questions
Part 0: Welcome, Assignment 1 & Assignment 2
Great job getting through Assignment 1!
Assignment 2 will be released late this week!
Part 1: The parts of a pointer
Look at this C code:
int n = 42;
int *p, *q;
p = &n;
*p = 5;
*q = 17;
q = p;
*q = 8;
Explain what does actually happen in the memory after each line in order?
Part 2: Functions and Pointers
Have a quick look at the following code:
#include <stdio.h>
void halve_values(int num_1, int num_2, int num_3);
int main(void) {
int num_1 = 4;
int num_2 = 10;
int num_3 = 16;
printf("Values before halved:\n");
printf("Num 1: %d\n", num_1);
printf("Num 2: %d\n", num_2);
printf("Num 3: %d\n", num_3);
halve_values(num_1, num_2, num_3);
printf("Values after halved:\n");
printf("Num 1: %d\n", num_1);
printf("Num 2: %d\n", num_2);
printf("Num 3: %d\n", num_3);
return 0;
}
void halve_values(int num_1, int num_2, int num_3) {
num_1 = num_1 / 2;
num_2 = num_2 / 2;
num_3 = num_3 / 2;
}
Note that function halve_values
doesn't seem to be working correctly.
Fixing the program
Use your knowledge of how to work with pointers to fix the program.
The following diagram may be useful as a guide for explaining how pointers work in functions:
Part 3: Struct pointers
Have a quick look at the following code which contains a struct book.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct book {
char title[100];
char author[100];
int year;
};
void modify_book(struct book book);
int main(void) {
struct book book;
strcpy(book.title, "To Kill a Mockingbird");
strcpy(book.author, "Harper Lee");
book.year = 1960;
printf("Before modification:\n");
printf("Title: %s, Author: %s, Year: %d\n", book.title, book.author, book.year);
modify_book(book);
printf("After modification:\n");
printf("Title: %s, Author: %s, Year: %d\n", book.title, book.author, book.year);
return 0;
}
void modify_book(struct book book) {
book.year = 1925;
strcpy(book.title, "The Great Gatsby");
strcpy(book.author, "F. Scott Fitzgerald");
}
Note that function modify_book
in this example doesn't seem to be working correctly.
Part 4: Command line arguments
Tutor demo!
In this section we'll have a look at command line arguments and how they can be used to pass input to a C program when it is executed.
We'll be using this starter code:
#include <stdio.h>
int main(int argc, char *argv[]) {
return 0;
}
The following diagram may be useful as a guide for explaining how command line arguments work:
Your turn!
In groups we will write pseudocode or a flowchart for one of the following programs:
Sum of Command Line Arguments: Write a C program that takes multiple integers as command-line arguments and prints their sum.
Count Characters in Command Line Arguments: Write a C program that counts the total number of characters in all the command-line arguments passed to it.
Reverse Command Line Arguments: Write a C program that prints all the command-line arguments passed to it in reverse order.
Check for Command Line Arguments: Write a C program that checks if any command-line arguments were provided except for the program name. If none were provided, print a message indicating so; otherwise, print the number of arguments.