Lesson 1 of 40
Lesson Progress: 0%
Pointers are one of the most powerful features in C. A pointer is a variable that stores the memory addressof another variable, instead of storing a value directly. Think of a pointer as a signpost that says "the data you are looking for is over there." You declare a pointer by writing the type followed by an asterisk *, like int* p. The address-of operator & gets the memory address of a variable, and the dereference operator * follows the pointer to access the value stored at that address. Pointers let you indirectly read and modify variables, which is essential for many advanced C techniques.
#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 x = 5; creates an integer variable x with value 5.int* p = &x; declares a pointer p that stores the memory address of x.*p dereferences the pointer to get the value stored at the address p points to, which is 5.%d format specifier prints the dereferenced integer value.#include <stdio.h>
int main() {
int x = 5;
int* p = &x;
*p = 12;
printf("%d\n", x);
return 0;
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
*p = 12; dereferences p and assigns a new value (12) to the memory location of x.x itself is changed to 12, even though the code never directly touches x.printf("%d\n", x); prints 12 because the value of x was changed through the pointer.#include <stdio.h>
int main() {
int x = 5;
int* p = &x;
int** q = &p;
printf("%d\n", **q);
return 0;
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
int** q.int** q = &p; stores the address of the pointer p itself.**q dereferences twice: first *q gets the pointer p, then the second * gets the value 5 from x.5 because all pointers ultimately lead to the same integer.#include <stdio.h>
int main() {
int x = 5;
int* p = &x;
int* r = p;
printf("%d\n", *r);
return 0;
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
int* r = p; makes r point to the same address as p, which is the address of x.p and r now point to the same variable x.*r dereferences r and prints the value of x, which is 5.#include <stdio.h>
int main() {
int x = 5;
int* p = &x;
int** q = &p;
printf("%d\n", **q);
return 0;
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
int** q = &p; stores the address of pointer p in q.**q goes through two levels of indirection to reach the value 5.5.#include <stdio.h>
int main() {
int x = 5;
int* p = &x;
*p = 30;
printf("%d\n", x);
return 0;
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
*p = 30; writes the value 30 into the memory address stored in p.p points to x, the variable x becomes 30.printf("%d\n", x); prints 30, confirming the change was made.Test Incomplete