Operations on Pointers in C
The main operations you can perform on pointers:- Pointer Assignment
- Dereferencing
- Pointer Arithmetic (
p++,p--,p+n,p-n) - Pointer to Pointer
a) Pointer Assignment
Store the address of a variable in a pointer:b) Dereferencing
Access the value stored at the address a pointer holds using*:
c) Pointer Arithmetic
Pointer arithmetic moves the pointer across memory positions based on the data type size. Forint *p (assuming int = 4 bytes): p++ moves 4 bytes forward.
| Operation | Effect |
|---|---|
p++ | Move to the next element |
p-- | Move to the previous element |
p + n | Move n elements forward |
p - n | Move n elements backward |
p++ — next element:
p-- — previous element:
p + n — move forward n elements:
p - n — move backward n elements: