Lesson 3 of 40
Lesson Progress: 0%
The dereference operator * (also called the indirection operator) is used with pointers to access the value stored at a memory address. If you have a pointer that stores the address of a variable, writing *pointerlets you read or modify that variable indirectly. It is called "dereferencing" because you are following the reference (address) to get to the actual data. The dereference operator can appear on the right side of an assignment (to read a value) or on the left side (to write a new value). Mastering dereferencing is key to understanding how pointers work in C.
#include <stdio.h>
int main() {
int x = 5;
int* p = &x;
printf("%d\n", *p);
return 0;
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
int* p = &x; makes p point to x by storing its address.*p dereferences the pointer to read the value stored at that address.5 is retrieved from x and printed.p alone would print a memory address, not 5.#include <stdio.h>
int main() {
int x = 5;
int* p = &x;
*p = 15;
printf("%d\n", x);
return 0;
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
*p = 15; writes the value 15 into the memory location p points to.x from 5 to 15, even though the code never mentions x directly.printf("%d\n", x); prints 15, confirming the change.*p = value; is a common way to modify a variable through a pointer.#include <stdio.h>
int main() {
int x = 5;
int* p = &x;
int y = *p;
printf("%d\n", y);
return 0;
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
int y = *p; reads the value at p (which is 5) and copies it into y.y is an ordinary int, not a pointer. It holds a copy of the value.y later would not affect x, because y is a separate variable.#include <stdio.h>
int main() {
int x = 5;
int* p = &x;
*p = *p + 3;
printf("%d\n", x);
return 0;
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
*p = *p + 3; reads the current value (5), adds 3, and writes the result back.x through the pointer, changing it from 5 to 8.*p + 3 dereferences p to get 5, then adds 3.x is 8, printed by printf.#include <stdio.h>
int main() {
int x = 5;
int* p = &x;
int* q = p;
*q = 20;
printf("%d\n", *p);
return 0;
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
int* q = p;, both p and q point to the same variable.*q = 20; dereferences q and writes 20 into the shared memory location.q and p point to the same place, *p now also reads 20.20, showing that the value was successfully changed through q.#include <stdio.h>
int main() {
int arr[3] = {10, 20, 30};
int* p = arr;
printf("%d\n", *p);
return 0;
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
arr acts like a pointer to its first element.int* p = arr; assigns the address of the first element to p. No & needed.*p dereferences the pointer and gives the first element 10.Test Incomplete