STEP 6 Review: Pointers, Structures, malloc, and Files

Review the key points with a cheat sheet, six code-reading questions, and a checklist of common mistakes.

① Key-point cheat sheet

Review the essentials of STEP 6 in these tables .

📍 Pointer basics

int x = 5;
int *p = &x;     // p holds x's address 
printf("%d\n", *p); // 5 (read the pointed-to value) 
*p = 100;          // x also becomes 100! 
  • &x: x's address
  • *p: the value pointed to by p (dereferencing)
  • int *pin * is part of a declaration; *p = 5in * dereferences a pointer

🏢 Structures (struct)

struct Point {
    int x, y;
};

struct Point p = { 3, 4 };
printf("%d %d\n", p.x, p.y); // 3 4

struct Point *pp = &p;
printf("%d\n", pp->x);  // 3 (-> is shorthand for (*pp).x) 
  • Related data is grouped together in a new type
  • Member access: for a structure value use .; for a pointer use ->

💾 malloc / free: dynamic memory

#include <stdlib.h>

int *a = malloc(sizeof(int) * 10);
if (a == NULL) { /* Allocation failed */  }

a[0] = 42;
free(a);             // Release the memory! 
a = NULL;            // Help prevent accidental double-free through this variable 
  • Allocate the amount needed at runtime (array length can be chosen at runtime)
  • When finished, free the allocation (or memory can leak)
  • On failure, NULL is returned

📁 File input and output

FILE *fp = fopen("data.txt", "r");
if (fp == NULL) { /* Could not open the file */  }

int n;
fscanf(fp, "%d", &n);
fclose(fp);          // Close the file! 
  • mode: "r"(read) / "w"(write, truncating existing contents) / "a"(append)
  • After fopen, always check for NULL ; when finished, call fclose

⚡ Bitwise operations

Symbol Meaning Example
&AND5 & 3 → 1
|OR5 | 3 → 7
^XOR5 ^ 3 → 6
~Complement ~5 → -6
<<Left shift 1 << 3 → 8
>>Right shift 8 >> 2 → 2
  • Evenness test: (n & 1) == 0
  • Multiply/divide by two: n << 1 / n >> 1
  • Logical &&and bitwise &operators differ

② Six code-reading questions

Execute the code mentally before choosing an answer. Read the explanation even if you get it wrong.

Q1. Modifying a value through a pointer

int x = 10;
int *p = &x;
*p = 99;
printf("%d\n", x);

What is the output?

10
99
An address (indeterminate value)
Compilation error
Explanation: p=&x makes p point to x. Assigning *p=99 modifies x itself, so the output is 99. This allows a function to change a caller-owned object through its address.

Q2. swap using pointers

void swap(int *a, int *b) {
    int t = *a;
    *a = *b;
    *b = t;
}
int x = 3, y = 7;
swap(&x, &y);
printf("%d %d\n", x, y);

What is the output?

3 7 (unchanged)
7 3
Indeterminate
Compilation error
Explanation: First t receives 3. Then *a=*b sets x to 7; finally *b=t sets y to 3. Output: 7 3. Passing values alone would swap only local copies; addresses let swap modify the originals.

Q3. Access through a structure pointer with ->

struct Point { int x, y; };

struct Point p = {10, 20};
struct Point *pp = &p;
pp->x = 99;
printf("%d %d\n", p.x, p.y);

What is the output?

10 20 (unchanged)
99 20
99 99
Compilation error
Explanation: pp->x means (*pp).x. Because pp points to p, assigning pp->x=99 changes p.x. The output is 99 20. Use -> for member access through a structure pointer.

Q4. malloc + free

#include <stdlib.h>

int *a = malloc(sizeof(int) * 3);
a[0] = 10; a[1] = 20; a[2] = 30;
int sum = a[0] + a[1] + a[2];
free(a);
printf("%d\n", sum);

What is the output?

60
Indeterminate (because free was called)
0
Compilation error
Explanation: sum is calculated before free and stored in a separate local variable. Freeing the allocation does not erase sum, so the output is 60. Reading a[0] after free would instead be undefined behavior.

Q5. Bitwise AND: testing evenness

for (int n = 1; n <= 5; n++) {
    if ((n & 1) == 0) printf("%d ", n);
}

What is the output?

1 3 5
2 4
1 2 3 4 5
Nothing
Explanation: n & 1 extracts the lowest bit. For these positive integers, (n & 1) == 0 identifies even numbers, printing 2 4. The test n%2==0 is equivalent here; modern compilers can optimize either form.

Q6. Copying a structure

struct P { int x, y; };

struct P a = {1, 2};
struct P b = a;       // Copy 
b.x = 99;
printf("%d %d %d %d\n", a.x, a.y, b.x, b.y);

What is the output?

1 2 99 2
99 2 99 2
99 99 99 99
Compilation error
Explanation: Structure assignment copies the member values. a and b are separate structures, so changing b.x leaves a.x unchanged. Output: 1 2 99 2. Pointer members would be shallow-copied, but these members are integers.

③ Common mistakes and tricky points

If an item feels familiar, return to its original lesson and review it.
  1. 1
    Used an uninitialized pointer and crashed
    int *p; *p = 5; leaves p without a valid target, causing undefined behavior when dereferenced. Initialize it to NULL or a valid address.
  2. 2
    Forgot free after malloc
    Release an allocation when you are finished. Memory leaks can exhaust resources in long-running programs.
  3. 3
    freeused a pointer after releasing its target (use-after-free)
    Accessing released memory is undefined behavior. After free, assigning p = NULL prevents accidental reuse through that variable; other aliases still need care.
  4. 4
    Used fopen's result without checking for NULL
    If opening fails, fopen returns NULL. Passing that to fscanf can cause a crash . Always check with if(fp == NULL).
  5. 5
    Forgot fclose
    Close the file when finished so buffered output is flushed and resources are released.
  6. 6
    Used the wrong operator with a structure pointer: .and ->were confused
    For a structure value, use p.x; for a pointer use pp->x. pp.x causes a compilation error.
  7. 7
    Logical &&and bitwise &operators
    (a == 1) & (b == 2)may appear to work because the comparisons yield truth values, but it lacks short-circuit evaluation. Combine conditions with &&.

④ Lessons to revisit when you get stuck

Use these links to return to the relevant explanation.
Lesson 36: Pointer basics &and *Review the meaning of Quiz (Pointers) Check understanding with related questions Structs (struct) Review . and -> member access Lesson 40: Unions Review shared memory for members Lesson 41: File input and output Review fopen, fclose, and file modes Lesson 42: Dynamic memory Review malloc, free, and NULL checks Bitwise Operators Review shifts, AND, OR, and XOR Cheat sheet Review pointers, structures, and malloc on one page