C PROGRAMMING · UNIT 4 · VISUAL COMPANION

Read the notes.
Watch the idea work.

Detailed notes in Google Docs.
Memory diagrams, animated calls and real C execution here.

Complete Google Docs notes6 step-by-step animations4 function categories16 runnable C programs
Learn one topic across two connected spaces.

The document holds the complete written material. This companion shows how objects and execution change. Follow a program link in the notes to select the matching example here.

UNIT 4 · VISUAL LESSON 01

Your notes and practical companion.

Use the Google Doc for definitions, syntax, complete explanations and written answers. Use this page to inspect memory, follow execution and run real C programs. Every program link in the notes selects its matching example here.

1 · Read

Open the Unit 4 document and study the current topic.

2 · Watch

Play an animation or move through it one step at a time. Describe which object changes.

3 · Run

Load the matching C example, edit it, and compare the output with your prediction.

Open the complete Unit 4 notes ↗

UNIT 4 · VISUAL LESSON 02

See why an address is not a copied value.

Track the separate objects x, p and y. Pause before *p = 25 and predict which values will change. The diagram uses an illustrative address.

ANIMATED EXPLANATION

From an address to a changed value

pointer memory and flow diagrampointer variable pnot set yetinteger variable x12no usable pointer yetp has its own storageillustrative address: 1000
  1. int x = 12;
  2. int *p = &x;
  3. int y = *p;
  4. *p = 25;
  5. /* x is now 25; p still holds &x */

1000 is a teaching address, not an address promised by the compiler. The first frame has no initialized pointer to use.

x contains 12. No pointer has been initialized yet.

Address stored in p—
Value stored in x—
Read the detailed notes ↗Run the matching C example

UNIT 4 · VISUAL LESSON 03

Two pointers can reach one object.

Copying p into q copies an address. Writing through q changes their shared target. Retargeting p later leaves q pointing to the original x.

Follow p and q through the writes

Read the detailed notes ↗Run the matching C example

UNIT 4 · VISUAL LESSON 04

Walk elements. Stop at one-past.

Choose a model element size and watch the address spacing change. The expression p + 1 moves one element, regardless of how many bytes that element occupies.

ANIMATED EXPLANATION

Walk the array, then stop at one-past

arithmetic memory and flow diagrama[0]101000a[1]201004a[2]301008a[3]401012a[4]501016one-pastNO VALUE1020p

Model base B = 1000. These numeric addresses and sizes are illustrative; the actual program uses real C pointers. Restart to compare a different element size.

p = a + 0. *p = 10. Offset 0 bytes; p - a = 0 elements.

Read the detailed notes ↗Run the matching C example

UNIT 4 · VISUAL LESSON 05

The array stays an array.

a in most expressions

Converts to the address of a[0]. Pointer traversal and indexed access can reach the same elements.

sizeof a

Measures the whole array in the caller; this expression does not convert a to a pointer.

Count plus endpoint

A function receiving a pointer needs the count separately. Stop before accessing one-past.

Read the detailed notes ↗Run the matching C example

UNIT 4 · VISUAL LESSON 06

Check the target before following the arrow.

Null

There is no object to reach. A null pointer must not be dereferenced.

Lifetime

An address must still identify a live object. Returning the address of an expired local object does not keep it alive.

Bounds and type

Non-null alone does not prove that the type, bounds or write access is valid.

Read the detailed notes ↗Run the matching C example

UNIT 4 · VISUAL LESSON 07

Give one task an interface.

Caller

Supplies argument values and waits for the call to finish.

Callee

Creates its parameters and local objects, then executes its body.

Return

Completes the call. A returned value can be used by the caller.

Read the detailed notes ↗Run the matching C example

UNIT 4 · VISUAL LESSON 08

Match the interface to the implementation.

PROTOTYPEint add(int a, int b);

Declares the two input types and the returned type.

CALLresult = add(x, y);

Evaluates arguments and supplies their values.

DEFINITION
int add(int a, int b)
{
    return a + b;
}

Performs the task and delivers its result.

Read the detailed notes ↗Run the matching C example

UNIT 4 · VISUAL LESSON 09

Follow one call from main and back.

Change the caller inputs, then step through argument evaluation, parameter creation, computation and return. The caller variables remain separate from the callee parameters.

ANIMATED EXPLANATION

Follow one complete call to add

call flow memory and flow diagramcaller: mainx = 2, y = 3callee: addnot calledreturned valuenone yetlocal total: not createdmain result: not assigned
  1. int x = 2, y = 3; Use the caller inputs above.
  2. result = add(x, y); begins the call
  3. int add(int a, int b) receives copies
  4. int total = a + b;
  5. return total;
  6. Resume main with the returned value assigned to result

These boxes represent each call’s objects and execution state. They do not prescribe a physical stack layout; a compiler may use registers or optimize storage.

main has x = 2 and y = 3. add has not been called.

Caller values—
Parameter values—
Returned value—
Caller result—
Read the detailed notes ↗Run the matching C example

UNIT 4 · VISUAL LESSON 10

Watch which arrows carry data.

Choose whether the function takes parameters and whether it returns a value. Printing is a side effect, so a void function can still do useful work.

EXPLORE THE FOUR INTERFACES

Which arrows carry data?

categories memory and flow diagramactual argumentsnonefunction taskbanner()returned valuenone (void)side effect: prints a banner

Category 1: banner(void) takes no parameters and returns no value. Printing is its side effect.

Run this category’s example
Actual arguments—
Function call—
Returned value—
Read the detailed notes ↗

UNIT 4 · VISUAL LESSON 11

A copied integer and a copied address have different effects.

Both runs start with x = 10. The left function changes its local integer parameter. The right function writes through a copied pointer to its caller’s x. C passes argument values in both runs.

ANIMATED EXPLANATION

Watch both calls side by side

passing memory and flow diagrammain: x10callee: nnot calledmain: x10callee: pnot calledPASS AN INT VALUEPASS AN ADDRESS VALUEchangeValue(x)changeAddress(&x)
void changeValue(int n)
{
    n = 99;
}
void changeAddress(int *p)
{
    *p = 99;
}

Left and right represent separate runs starting with x = 10. The right-hand pointer holds the address of its caller’s x, represented here by &x.

Both callers start with x = 10. No callee parameter exists yet.

Caller after value passing—
Caller after address passing—
Read the detailed notes ↗Run the value versionRun the address version

UNIT 4 · VISUAL LESSON 12

Save a value before replacing it.

The temporary value prevents the first write from destroying a value needed by the second. Predict the caller’s final values, then run both swap versions.

Trace the three assignments through pointers

Read the detailed notes ↗Run the matching C example

UNIT 4 · VISUAL LESSON 13

Explain what each write can reach.

p = &y;

Changes the pointer object p; it does not move q or overwrite x.

*p = 99;

Changes the live writable object currently identified by p.

n = 99; in a value parameter

Changes only the separate local integer n.

Read the complete error and correction table ↗

UNIT 4 · VISUAL LESSON 14

Run the program linked from your notes.

Choose an example and load it into the embedded C compiler. Select I/O inside the compiler, check the sample STDIN, and press Run. Change the input or edit the source to explore a different case.

Sample input (STDIN)

No input needed.

Expected output

Before: x = 12, *p = 12
After: x = 25, *p = 25

Load the compiler when you’re ready to practise.

Your C workspace

Edit real C code and see real compiler output.

Compiler provided by OneCompiler. Opening the editor connects to that service; code and input you run are processed there. If the embed is unavailable, open the C compiler in a new tab and paste the source below.

View / copy the selected source
#include <stdio.h>
int main(void)
{
    int x = 12;
    int *p = &x;
    printf("Before: x = %d, *p = %d\n", x, *p);
    *p = 25;
    printf("After: x = %d, *p = %d\n", x, *p);
    return 0;
}
Browse all 16 Unit 4 programs

UNIT 4 · VISUAL LESSON 15

Explain the diagram after closing it.

The Google Doc contains all 12 written model answers. Draw your own memory diagram and explain what an address-based call changes, why one-past is only an endpoint, and how a return value reaches the caller.

Open the written revision answers ↗

UNIT 4 · VISUAL LESSON 16

Apply what you saw.

1. What does int *p = &x; store in p?
2. Which expression accesses the target of p?
3. In int *p, q; which variables are pointers?
4. If sizeof(int) is 4 in this model, p + 2 advances by…
5. For int a[5], what can be done with a + 5?
6. If p and q point into the same int array, q - p counts…
7. Which format correctly prints an object pointer p?
8. What does a null check alone establish?
9. Which is an explicit no-parameter prototype?
10. What is add(2, 3) in result = add(2, 3);?
11. Which category is void printSquare(int side)?
12. After changeValue(x) executes n = 99, caller x initially 10 is…
13. How does changeAddress(&x) with *p = 99 change caller x?
14. Inside void f(int a[], int n), sizeof a measures…

UNIT 4 · VISUAL LESSON 17

Keep your reading and practice connected.

The full Unit 4 notes and references are maintained in Google Docs. This page provides the interactive explanations and C practice for the same syllabus.

Open the notes in Google Docs ↗

← Unit 3C Programming guide