美文网首页
6.8a Pointer arithmetic and arra

6.8a Pointer arithmetic and arra

作者: Closears | 来源:发表于2017-08-26 21:23 被阅读19次

原完整教程链接: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.

相关文章

网友评论

      本文标题:6.8a Pointer arithmetic and arra

      本文链接:https://www.haomeiwen.com/subject/ycuvdxtx.html