Pointers can take part in a limited kind of arithmetic. The rules look strange at first: adding 1 to a pointer does not add 1 to the address. This section explains the scaling rule and lists the operations that are and are not permitted.
The scaling rule
address of (p+n)=address in p+n×sizeof(type)
Pointer type
sizeof the target
p + 1 moves the address by
char *
1
1 byte
int *
4
4 bytes
float *
4
4 bytes
double *
8
8 bytes
So if a float pointer holds the address 65526, then after ptr++ it holds 65530.
Allowed and not allowed
Operation
Example
Allowed?
Meaning
Add or subtract an integer
p + 3, p - 1, p++, --p
Yes
Move by that many elements
Subtract two pointers into the same array
q - p
Yes
How many element positions apart they are (difference of the indexes)
Compare two pointers into the same array
p == q, p < q
Yes
Relative positions
Compare with NULL
p != NULL
Yes
Test for a valid pointer
Assign pointers of the same type
p = q;
Yes
Both now point to the same object
Add two pointers
p + q
No
Meaningless
Multiply or divide a pointer
p * 2, p / 5
No
Meaningless
Arithmetic on the values pointed to is ordinary arithmetic: *p1 + *p2, *p1 * *p2, *p1 / *p2. In the last one, leave a space between / and *, because /* starts a comment.
Precedence with * and ++
Expression
Meaning
*p++
Use the value *p, then move the pointer p to the next element
(*p)++
Increase the value pointed to; the pointer does not move
*++p
Move the pointer first, then use the value at the new position
++*p
Increase the value pointed to, then use it
Key takeaways
p + n advances by n elements: n × sizeof(type) bytes.
Invalid: pointer + pointer, pointer × or ÷ anything.
*p++ moves the pointer; (*p)++ changes the value.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Given int a[5] = {2, 4, 6, 8, 10}; int *p = a + 1;, evaluate *p, *(p + 2) and p[-1], and explain each.
Show worked answer
p points to a[1]. *p is the element there, 4. *(p + 2) is the element two positions after a[1], which is a[3], 8. p[-1] means *(p - 1), the element one position before a[1], which is a[0], 2. All three stay inside the array, so they are valid.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/pointers/pointer-arithmetic/ to read this page.