1 · Read
Open the Unit 4 document and study the current topic.
C PROGRAMMING · UNIT 4 · VISUAL COMPANION
Detailed notes in Google Docs.
Memory diagrams, animated calls and real C execution here.
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
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.
Open the Unit 4 document and study the current topic.
Play an animation or move through it one step at a time. Describe which object changes.
Load the matching C example, edit it, and compare the output with your prediction.
UNIT 4 · VISUAL LESSON 02
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
int x = 12;int *p = &x;int y = *p;*p = 25;/* 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.
UNIT 4 · VISUAL LESSON 03
Copying p into q copies an address. Writing through q changes their shared target. Retargeting p later leaves q pointing to the original x.
UNIT 4 · VISUAL LESSON 04
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
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.
UNIT 4 · VISUAL LESSON 05
a in most expressionsConverts to the address of a[0]. Pointer traversal and indexed access can reach the same elements.
sizeof aMeasures the whole array in the caller; this expression does not convert a to a pointer.
A function receiving a pointer needs the count separately. Stop before accessing one-past.
UNIT 4 · VISUAL LESSON 06
There is no object to reach. A null pointer must not be dereferenced.
An address must still identify a live object. Returning the address of an expired local object does not keep it alive.
Non-null alone does not prove that the type, bounds or write access is valid.
UNIT 4 · VISUAL LESSON 07
Supplies argument values and waits for the call to finish.
Creates its parameters and local objects, then executes its body.
Completes the call. A returned value can be used by the caller.
UNIT 4 · VISUAL LESSON 08
int add(int a, int b);Declares the two input types and the returned type.
result = add(x, y);Evaluates arguments and supplies their values.
int add(int a, int b)
{
return a + b;
}Performs the task and delivers its result.
UNIT 4 · VISUAL LESSON 09
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
int x = 2, y = 3; Use the caller inputs above.result = add(x, y); begins the callint add(int a, int b) receives copiesint total = a + b;return total;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.
UNIT 4 · VISUAL LESSON 10
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
Category 1: banner(void) takes no parameters and returns no value. Printing is its side effect.
Run this category’s exampleUNIT 4 · VISUAL LESSON 11
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
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.
UNIT 4 · VISUAL LESSON 12
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.
UNIT 4 · VISUAL LESSON 13
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 parameterChanges only the separate local integer n.
UNIT 4 · VISUAL LESSON 14
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.
Load the compiler when you’re ready to practise.
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.
#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;
}
UNIT 4 · VISUAL LESSON 15
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
UNIT 4 · VISUAL LESSON 17
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 ↗