Pointers in C
Pointer basics, pointer arithmetic, pointers with arrays/strings, double pointers, and void pointers.
What You Will Learn in Pointers in C
A pointer in C is a variable that stores the memory address of another variable, enabling direct memory manipulation and efficient passing of data.
- Pointer declaration: `int *ptr;` — stores address of an int variable.
- `&` (address-of operator): `ptr = &var;` — assigns address of `var` to `ptr`.
- `*` (dereference operator): `*ptr` accesses the value at the stored address.
- Pointer arithmetic: `ptr++` moves to next element (increments by `sizeof(type)`).
- NULL pointer: `int *ptr = NULL;` — initialise pointers to NULL to avoid undefined behaviour.
- Function pointers, double pointers, pointers to arrays are advanced pointer concepts.
Syntax
int var = 10;
int *ptr = &var; // ptr holds address of var
printf("%d", var); // 10
printf("%p", ptr); // address (e.g., 0x7ffd...)
printf("%d", *ptr); // 10 (dereference)
*ptr = 20; // change var through pointer
printf("%d", var); // 20Complete Code Example
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 5, y = 10;
printf("Before: x=%d, y=%d\n", x, y);
swap(&x, &y);
printf("After: x=%d, y=%d\n", x, y);
// Before: x=5, y=10
// After: x=10, y=5
// Pointer arithmetic
int arr[] = {10, 20, 30};
int *p = arr;
for (int i = 0; i < 3; i++)
printf("%d ", *(p + i)); // 10 20 30
return 0;
}Example
`int *p = &x;` makes `p` point to `x`; `(*p)++` increments `x` through the pointer.
Expected Exam Questions — Pointers in C
Q1.What is the difference between `*ptr` and `&var`?
Q2.What is a dangling pointer?
Q3.What is the difference between call-by-value and call-by-reference in C?
🔘 MCQ Practice — Pointers in C
MCQ 1.What does `int *p = &x;` declare?
✓ Correct Answer: p is a pointer holding the address of x
Download Pointers in C PDF Notes
Get the complete Pointers in C notes as a PDF — free for enrolled students, or browse our public study materials library.