Take a moment to think about this
This one's tougher than the fundamentals lesson — you'll need to apply function decomposition, recursion, pointer manipulation, and structs all at once. As you solve the tasks, we'll build the habit of writing a function's prototype first, then filling in the implementation. When working with pointers, correctly dereferencing (*) the memory address matters a lot. And when passing a struct as a function parameter, the goal is to get you thinking carefully about whether it should be passed by value or by pointer.
Exercises
Task 1: Write an int factorial(int n) function using recursion, then use a loop in main() to print the factorial of each number from 1 to 10. Task 2: Write an int* findMax(int arr[], int size) function that returns a pointer (address) to the largest value in the array — dereference it in main() and print the value. Task 3: Write a void swap(int *a, int *b) function using pointer parameters — call it in main() to swap two variables. Task 4: Define struct Student { char name[30]; int age; float gpa; }, then write a void printStudent(struct Student s) function that takes the struct as a parameter and prints all its fields in a neatly formatted way.
Code Example
// Task 3 starter skeleton
#include <stdio.h>
void swap(int *a, int *b) {
// TODO: *a နဲ့ *b ရဲ့ value ကို swap ပါ
}
int main() {
int x = 5, y = 10;
swap(&x, &y);
printf("x = %d, y = %d\n", x, y);
return 0;
}
// Task 4 starter skeleton
#include <stdio.h>
#include <string.h>
struct Student {
char name[30];
int age;
float gpa;
};
void printStudent(struct Student s) {
// TODO: s.name, s.age, s.gpa ကို print ပါ
}
int main() {
struct Student s1 = {"Aung Aung", 20, 3.75};
printStudent(s1);
return 0;
}Once all 4 tasks run correctly, you'll know you can implement all three yourself — recursion, pointer swap, and struct pass-by-value logic.5-Minute Challenge
Set a 5-minute timer and write Task 2 yourself — focus on the int* return type and how to return the address of an array element.
A Quick Warning
When combining pointers and structs, always be careful not to dereference a NULL pointer — it can cause a crash.