tailieunhanh - The C++ Programming Language Third Edition phần 2

* Tiền tố dereferences điều hành một con trỏ để * đó p là nhân vật chỉ bởi ppp, và + + tăng con trỏ để nó đề cập đến phần tử tiếp theo của mảng. Không có lý do cố hữu lý do tại sao một phiên bản nhanh hơn. Với trình biên dịch hiện đại, mã giống hệt nhau nên được tạo ra cho cả hai ví dụ (xem § 5,9 [8]). | Section Navigating Arrays 93 is equivalent to a traversal using a pointer void fp char v for char p v pl 0 p use p The prefix operator dereferences a pointer so that p is the character pointed to by p and increments the pointer so that it refers to the next element of the array. There is no inherent reason why one version should be faster than the other. With modern compilers identical code should be generated for both examples see 8 . Programmers can choose between the versions on logical and aesthetic grounds. The result of applying the arithmetic operators - or -- to pointers depends on the type of the object pointed to. When an arithmetic operator is applied to a pointer p of type T p is assumed to point to an element of an array of objects of type T p 1 points to the next element of that array and p -1 points to the previous element. This implies that the integer value of p 1 will besizeof T larger than the integer value ofp. For example executing inelude iostream int main int vi 10 short vs 10 std cout vi 0 Scvi 1 n std cout vs 0 S U n produced 0x 7fffaef0 0x 7ffaef4 0x 7fffa e dc 0x 7 fffa e de using a default hexadecimal notation for pointer values. This shows that on my implementation sizeof short is 2 and sizeof int is 4. Subtraction of pointers is defined only when both pointers point to elements of the same array although the language has no fast way of ensuring that is the case . When subtracting one pointer from another the result is the number of array elements between the two pointers an integer . One can add an integer to a pointer or subtract an integer from a pointer in both cases the result is a pointer value. If that value does not point to an element of the same array as the original pointer or one beyond the result of using that value is undefined. For example void f int v1 10 int v2 10 int i1 v1 5 - v1 3 i1 2 int i2 v1 5 - v2 3 result undefined 94 Pointers Arrays and Structures Chapter 5 int pl v2 2 pl v2 2 int p2 v 2 -2 p2 .

TỪ KHÓA LIÊN QUAN
crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.