A
A
Alexander2019-02-10 12:21:17
C++ / C#
Alexander, 2019-02-10 12:21:17

How does C pointer arithmetic work?

At the exam in programming (C language) there was such a question
"If c is an array of char, then what is the difference (c + 3) - c (in bytes)?".
The correct answer is 3.
Please explain how 3 is obtained?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2019-02-10
@ErshoVV

When you subtract pointers, the result is the number of elements of the specified type between those pointers. In your case, 3 char seems to assume that sizeof(char) == 1. In general, this is not the case, the correct answer is 3*sizeof(char).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question