Addresses, the & and * operators, NULL pointers, and call by address
Grade XII • Computer Science ⏱️ ~20 minBrief Intro — Pointers
A pointer is one of the most powerful features of C, giving you a way to work directly with a computer's memory addresses. Rather than just holding a value, a pointer tells the program exactly where that data lives.
In this activity, you will master the address (&) and dereference (*) operators so you can manage memory with precision and efficiency.
Every variable lives at a unique memory address; pointers allow us to store and manipulate these addresses directly.
Task 1: Addresses in Memory
Every variable is a memory location, and every memory location has its address defined. That address can be accessed using the ampersand (&) operator, which denotes an address in memory.
&var gives the memory address where var is stored.
Task 2: Pointer Definition
A pointer is a variable whose value is the address of another variable, i.e., the direct address of a memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address.
A pointer stores an address, not a regular value.
Task 3: Declaring Pointers
The general form of a pointer declaration is:
type *var-name;
Valid examples: int *ip; (pointer to an integer), double *dp;, float *fp;, char *ch;. The asterisk in the declaration designates the variable as a pointer.
In a declaration, * marks a pointer variable; the type says what it points to.
Task 4: Pointer Types
The actual data type of the value of all pointers is the same — a long hexadecimal number that represents a memory address. The only difference between pointers of different data types is the data type of the variable that the pointer points to.
All pointers hold an address; int *ip vs char *ch differ only in what they point at.
Working with pointers involves defining them, assigning addresses, and using dereferencing to access the data they point to.
Watch pc point to c. Press a button to run that statement.
Task 5: The Three Steps
To use pointers, we frequently do three operations:
* operator, which returns the value of the variable located at the address specified by its operand.Define → assign (ip = &var;) → access (*ip).
Task 6: The Basic Trace
With int var = 20; int *ip; ip = &var;, the program prints the address of var and the address stored in ip (the same value), then Value of *ip variable: 20.
&var and ip hold the same address; *ip gives 20.
Task 7: The Working Example
Trace the notes' working example: c = 22; pc = &c; then c = 11; — *pc reads 11 because it reads whatever is at c's address — then *pc = 2; — this changes the value at the memory location pointed to by pc to 2, so c becomes 2. Use the visualizer above to step through these statements.
After *pc = 2;, c is 2: the pointer writes through to the pointed-to variable.
Task 8: Common Mistakes
Wanting pc to point to the address of c:
pc = c; // Error, pc is address but c is not *pc = &c; // Error, &c is address but *pc is not pc = &c; // Valid, both &c and pc are addresses *pc = c; // Valid, both c and *pc are values
Also: int *p = &c; is equivalent to int *p; p = &c; — in both cases we create pointer p (not *p) and assign &c to it.
Match the two sides of an assignment: address into a pointer (pc = &c;), value into the pointed-to location (*pc = c;).
Pointers become truly powerful when we use them to pass data by address to functions or use NULL to signal that a pointer points to nothing.
Task 9: The NULL Pointer
It is good practice to assign a NULL value to a pointer when you do not have an exact address. A pointer assigned NULL is called a null pointer. NULL is a constant with a value of zero defined in several standard libraries. With int *ptr = NULL;, the program prints The value of ptr is 0.
NULL has value 0; a null pointer prints as 0.
Task 10: Why Address 0
In most operating systems, programs are not permitted to access memory at address 0 — that memory is reserved by the operating system. By convention, if a pointer contains the null (zero) value, it is assumed to point to nothing. To check for a null pointer: if(ptr) succeeds if ptr is not null; if(!ptr) succeeds if ptr is null.
Address 0 signals "points to nothing"; use if(ptr) / if(!ptr) to test.
Step through both versions of swap. Watch which frame's values change.
Task 11: Passing by Reference / Address
Passing an argument by reference or by address enables the passed argument to be changed in the calling function by the called function. In the call-by-reference method, the address of the variable is passed as the parameter, and the value of the actual parameter can be modified by the formal parameter.
By default C passes arguments by value; passing an address lets the function change the caller's variable.
Task 12: Swap by Address Trace
void swap(int *a, int *b) {
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
In swap(&m, &n) with m = 22, n = 44, the parameters a and b hold the addresses of m and n. They are not copied — the addresses are used to access and change the variables. Output:
Values before swap m = 22 and n = 44 Values after swap a = 44 and b = 22
m and n become 44 and 22 — the caller's variables DO change when addresses are passed.
Ready to test your knowledge?
Take the Assessment →