Programming Fundamentals
Tutorial Overview:
- Part 1: The parts of a pointer
- Part 2: Functions, diagrams and pointers
- Part 3: Struct pointers
- Part 4: Command line arguments
Check in week
Reminder: This week is a check-in week, so please make sure to stay around in the lab time for your one-on-one with one of your Tutors.
Part 0: Welcome, Assignment 1 & Assignment 2
Great job getting through Assignment 1!
Assignment 2 will be released late Week 7!
Part 1: The parts of a pointer
In this section, tutors will lead a short revision section on pointers, and then walk through a demonstration simulating how pointers manipulate memory.
This tool will be used to further explore pointers:
What will happen when each of the following statements is executed (in order)?
int n = 42;
int *p;
int *q;
p = &n;
*p = 5;
*q = 17;
q = p;
*q = 8;
Fill in the values of each variable in the below visual at each point in the code execution.
01: int n = 42;02: int *p;03: int *q;04: p = &n;05: *p = 5;06: *q = 17;07: q = p;08: *q = 8;
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;
strncpy(book.title, "The Great Gatsby", 100);
strncpy(book.author, "F. Scott Fitzgerald", 100);
}
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.