6.8a Pointer arithmetic and arra
2017-08-26 本文已影响19人
Closears
原完整教程链接:6.8a Pointer arithmetic and array indexing
1.
/*
The C++ language allows you to perform integer addition or
subtraction operations on pointers.
Note that ptr + 1 does not return the memory address after ptr, but
the memory address of the next object of the type that ptr points
to. If ptr points to an integer (assuming 4 bytes), ptr + 3 means 3
integers (12 bytes) after ptr. If ptr points to a char, which is always
1 byte, ptr + 3 means 3 chars (3 bytes) after ptr.
When calculating the result of a pointer arithmetic expression, the
compiler always multiplies the integer operand by the size of the
object being pointed to. This is called **scaling**.
*/
2.
// Generalizing, array[n] is the same as *(array + n), where n is an
// integer. The subscript operator [] is there both to look nice and for
// ease of use.