PSUC Lab 11 — Memory Addresses & Pointers
Active Logic: Pointers & Memory Dereferencing
Master C pointer variables: address-of operator (&var), indirection/dereference operator (*ptr), pointer arithmetic, pointer array traversal, and substring pointer search (strstr).
Pointer Rules
| Concept | Syntax / Rule | Key Trap / Pitfall |
|---|---|---|
| Address Operator | &var (gives memory address) | Do not dereference uninitialized NULL pointers! |
| Dereference Operator | *ptr (fetches stored value) | *ptr gets value; ptr gets memory address |
| Pointer Arithmetic | ptr++ (advances by sizeof(type)) | (int*)ptr + 1 adds 4 bytes to memory address! |
| Array Equivalence | arr[i] == *(arr + i) | Array name acts as a constant pointer to 0th element |
🎯 Learning Philosophy: Active Logic Building
Test your pointer logic across 3 Interactive Learning Modes:
- 🧠 Tab 1 (Logic & Hints): Memory layout & pointer addresses.
- 🧩 Tab 2 (Logic Puzzle): Complete missing dereferences
*ptr& test live address visualizers! - 💻 Tab 3 (C Solution): Reference verified C code solutions.
Solve pointer logic puzzles before peeking at C reference code!
Exercise 1 of 5
Access & Add Two Integers Using Pointers
Stores addresses of two integers in pointer variables p1 and p2 and computes sum via dereferencing (*p1 + *p2).
Address & Dereference Syntax
- Address assignment:
int *p1 = &num1; - Dereference addition:
sum = *p1 + *p2;
Interactive Logic Hints
💡 Hint 1: What does %p print in printf()? ▼
%p prints the hexadecimal memory address stored in the pointer variable!🧩 Complete Pointer Dereference Logic
int num1 = 15, num2 = 25;
int *p1 = num1;
int *p2 = &num2;
int sum = ;
int *p1 = num1;
int *p2 = &num2;
int sum = ;
Select statements above and click Check!
📍 Pointer Address Simulator
Memory addresses & dereferenced sum...
add_pointers.cReference C Code
#include <stdio.h>
int main() {
int num1 = 15, num2 = 25, sum;
int *p1 = &num1;
int *p2 = &num2;
sum = *p1 + *p2;
printf("p1 = %p, *p1 = %d\n", (void*)p1, *p1);
printf("p2 = %p, *p2 = %d\n", (void*)p2, *p2);
printf("Sum (*p1 + *p2) = %d\n", sum);
return 0;
}
Exercise 2 of 5
Greatest & Smallest Among Three Numbers
Compares three numbers using pointers *pa, *pb, and *pc.
Pointer Comparison Rules
- Compare values:
if (*pa >= *pb && *pa >= *pc) max = *pa; - Store pointer to max:
int *pMax = pa;
Interactive Logic Hints
💡 Hint 1: What is the difference between comparing pa vs *pa? ▼
pa > pb compares memory addresses! *pa > *pb compares the actual numbers stored at those addresses! Always use *!🧩 Complete Pointer Min/Max Comparison
int *pMax = pa;
if ( > *pMax) pMax = pb;
if (*pc > ) pMax = pc;
printf("Max = %d", );
if ( > *pMax) pMax = pb;
if (*pc > ) pMax = pc;
printf("Max = %d", );
Select statements above and click Check!
📊 Pointer Min/Max Comparator
Min/Max pointer result...
pointer_min_max.cReference C Code
#include <stdio.h>
int main() {
int a = 45, b = 89, c = 23;
int *pa = &a, *pb = &b, *pc = &c;
int *pMax = pa;
if (*pb > *pMax) pMax = pb;
if (*pc > *pMax) pMax = pc;
int *pMin = pa;
if (*pb < *pMin) pMin = pb;
if (*pc < *pMin) pMin = pc;
printf("Greatest = %d, Smallest = %d\n", *pMax, *pMin);
return 0;
}
Exercise 3 of 5
Determine String Length Using Pointer
Calculates string length by advancing a character pointer ptr++ until reaching null terminator '\0'.
Pointer Subtraction Length Formula
- Initialize:
char *ptr = str; - Advance:
while (*ptr != '\0') ptr++; - Length:
length = ptr - str;(Pointer subtraction!)
Interactive Logic Hints
💡 Hint 1: How does ptr - str calculate length? ▼
Subtracting base pointer
str from end pointer ptr gives the exact number of elements between them!🧩 Complete Pointer Strlen Loop
char *ptr = str;
while ( != '\0') {
;
}
int len = ptr str;
while ( != '\0') {
;
}
int len = ptr str;
Select statements above and click Check!
📏 Pointer Strlen Engine
Length status...
ptr_strlen.cReference C Code
#include <stdio.h>
int main() {
char str[] = "Pointer Memory Traversal";
char *ptr = str;
while (*ptr != '\0') {
ptr++;
}
printf("Length of '%s' = %ld\n", str, ptr - str);
return 0;
}
Exercise 4 of 5
Compute Sum of Array Elements Using Pointer
Traverses an array using pointer arithmetic (*(ptr + i)) to calculate total sum.
Array Pointer Traversal
int *ptr = arr;- Accumulation:
sum += *(ptr + i)orsum += *ptr; ptr++;
Interactive Logic Hints
💡 Hint 1: What is the difference between *ptr++ vs (*ptr)++? ▼
*ptr++ advances pointer to NEXT memory location! (*ptr)++ increments value in current memory location!🧩 Complete Pointer Array Sum Loop
int *ptr = arr, sum = 0;
for (i = 0; i < n; i++) {
sum += ;
}
for (i = 0; i < n; i++) {
sum += ;
}
Select statements above and click Check!
➕ Pointer Array Sum Visualizer
Sum result...
ptr_array_sum.cReference C Code
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
int n = 5, i, sum = 0;
int *ptr = arr;
for (i = 0; i < n; i++) {
sum += *(ptr + i);
}
printf("Sum of array elements = %d\n", sum);
return 0;
}
Exercise 5 of 5
Substring Pointer Search (Returning Pointer to Match)
Searches a substring pattern in text and returns a pointer pointing to the first character of occurrence.
Pointer strstr() Function
- Function:
char *ptr = strstr(str, sub); - Returns NULL if substring not found!
- Index position =
ptr - str
Interactive Logic Hints
💡 Hint 1: What does strstr() return on match? ▼
It returns a pointer pointing to the exact memory address where the substring starts!
🧩 Complete Substring Pointer Function
char *match = (str, sub);
if (match NULL) {
int pos = match - str;
printf("Found at index %d", pos);
}
if (match NULL) {
int pos = match - str;
printf("Found at index %d", pos);
}
Select statements above and click Check!
🔎 Substring Pointer Match Visualizer
Substring match result...
ptr_substring.cReference C Code
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "learning c pointers is fun";
char sub[] = "pointers";
char *match = strstr(str, sub);
if (match != NULL) {
printf("Substring found at index %ld: '%s'\n", match - str, match);
} else {
printf("Substring NOT found.\n");
}
return 0;
}
Knowledge Check
Test Your Pointer & Memory Mastery
Question 1 of 5
Loading question...