CTF: Structs
6 challenges — Week 8
Structs Challenges
| 6 challenges | Week 8 | Week 8 (Structs, typedef, Unions, and the Arrow Operator) |
Struct and union challenges covering struct definition with typedef, dot vs arrow operator for member access, passing structs by value and by pointer, arrays of structs, sorting struct arrays by field, and union semantics. These exercises build on the pointer and dynamic memory foundations from Weeks 6-7 and prepare students for the Stock Portfolio Manager (Lab 7).
Difficulty Breakdown
| Level | Count |
|---|---|
| Beginner | 3 |
| Intermediate | 3 |
Challenges
1. Struct Definition
| Difficulty: Beginner | Time: ~8 min | Type: function |
Define a Book struct using typedef with the following members: title (char[100]), author (char[50]), pages (int), price (double). Then write two functions:
- Book createBook(const char *title, const char *author, int pages, double price) — returns a Book initialized with the given values using …
Concepts: struct definition with typedef, member access with dot operator, strcpy for string member initialization, returning a struct from a function, passing a struct by value to a function
2. Dot Vs Arrow
| Difficulty: Beginner | Time: ~10 min | Type: bare code |
Given the following struct definition and variable declarations, evaluate each expression. Write the value that each expression produces, or write “address” if the expression evaluates to a memory address.
typedef struct { char name[20]; int id; double gpa; } Student;
Student s = {“…
Concepts: dot operator on struct values, arrow operator on struct pointers, equivalence of p->field and (*p).field, address-of operator with struct variables, address-of operator with struct members
3. Struct Function Parameter
| Difficulty: Intermediate | Time: ~12 min | Type: function |
Given the following Student struct, write two functions that demonstrate the difference between passing a struct by value and by pointer:
typedef struct { char name[50]; int id; double gpa; } Student;
Function 1: void printStudent(Student s)
- Prints: “Name: [name], ID: [id], GPA:…
Concepts: passing struct by value (copy semantics), passing struct by pointer (reference semantics), arrow operator for modifying struct through pointer, dot operator for reading struct members, address-of operator (&) to pass struct pointer
4. Array Of Structs
| Difficulty: Intermediate | Time: ~15 min | Type: function |
Using the Student struct below, write a program that:
- Creates an array of 5 Students with the following data:
- {“Alice”, 1001, 3.9}
- {“Bob”, 1002, 3.2}
- {“Carol”, 1003, 3.95}
- {“Dave”, 1004, 2.8}
- {“Eve”, 1005, 3.7}
- Writes a function void printAllStudents(Student …
Concepts: declaring and initializing an array of structs, iterating through a struct array with index, dot operator on array elements: students[i].field, returning a pointer to an array element, arrow operator on returned pointer
5. Sort Structs By Field
| Difficulty: Intermediate | Time: ~15 min | Type: function |
Using the Stock struct below, write two sorting functions:
typedef struct { char symbol[10]; char company[50]; double price; } Stock;
Function 1: void sortBySymbol(Stock stocks[], int count)
- Sorts the array alphabetically by symbol (ascending, using strcmp)
- Use selection sor…
Concepts: sorting an array of structs by a string field using strcmp, sorting an array of structs by a numeric field, selection sort algorithm applied to struct arrays, swapping entire structs (not just individual fields), strcmp return value interpretation (negative, zero, positive)
6. Union Basics
| Difficulty: Beginner | Time: ~8 min | Type: bare code |
Trace the following code involving a union. For each printf statement, write the output or write “garbage/undefined” if the value is meaningless.
#include
union Value { int i; double d; char c; };
int main(void) { union Value v;
// --- Part A: sizeof ---
prin...
Concepts: union definition and declaration, sizeof union equals sizeof largest member, all union members share the same memory, only the last-written member has a meaningful value, type punning through union members