MCS-201 · Unit 7

Pointers

Section 3 of 7

Block 2 · Unit 7Pointers7 min · Section 3 of 7

7.3Pointer Arithmetic

Covers IGNOU MCS-201 §7.5

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)\text{address of } (p + n) = \text{address in } p + n \times \text{sizeof(type)}

Pointer typesizeof the targetp + 1 moves the address by
char *11 byte
int *44 bytes
float *44 bytes
double *88 bytes

So if a float pointer holds the address 65526, then after ptr++ it holds 65530.

Allowed and not allowed

OperationExampleAllowed?Meaning
Add or subtract an integerp + 3, p - 1, p++, --pYesMove by that many elements
Subtract two pointers into the same arrayq - pYesHow many element positions apart they are (difference of the indexes)
Compare two pointers into the same arrayp == q, p < qYesRelative positions
Compare with NULLp != NULLYesTest for a valid pointer
Assign pointers of the same typep = q;YesBoth now point to the same object
Add two pointersp + qNoMeaningless
Multiply or divide a pointerp * 2, p / 5NoMeaningless

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 ++

ExpressionMeaning
*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
*++pMove the pointer first, then use the value at the new position
++*pIncrease the value pointed to, then use it

Key takeaways

  • p + n advances by n elements: n × sizeof(type) bytes.
  • Valid: pointer ± integer, pointer − pointer, comparisons (same array), assignment.
  • 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 1 · multiple choice

A double *p holds the address 2000 (double is 8 bytes). What address does p + 3 hold?

Show answer

Answer: C. 2024

2000 + 3 × 8 = 2024. Adding 3 directly (2003) ignores the scaling by the element size.

Question 2 · multiple choice

Which operation on two pointers p and q into the same array is NOT allowed?

Show answer

Answer: D. p + q

Adding two addresses has no meaning and is rejected by the compiler. Subtraction and comparison are valid within one array.

Question 3 · multiple choice

For int a[6];, what is the value of &a[5] - &a[2]?

Show answer

Answer: A. 3

Subtracting pointers gives the difference of the indexes, 5 − 2 = 3: you must step forward 3 times to go from a[2] to a[5]. It is not the number of elements strictly between them (a[3] and a[4], which is 2), and not the number of bytes (12).

Question 4 · multiple choice

What does this program print?

C · star_plus.c
#include <stdio.h>

int main(void)
{
    int a[3] = {5, 10, 15};
    int *p = a;
    (*p)++;
    p++;
    printf("%d %d\n", a[0], *p);
    return 0;
}
Show answer

Answer: C. 6 10

(*p)++ increases a[0] from 5 to 6; p++ then moves the pointer to a[1], whose value is 10.

Question 5 · multiple choice

What does *p++ do?

Show answer

Answer: B. Gives the value pointed to, then advances the pointer to the next element

Postfix ++ applies to p, but its effect comes after the current *p is used. Changing the value itself needs (*p)++.

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.

↑ ↓ move↵ openTab switch type