Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 102 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
102
Dung lượng
328,54 KB
Nội dung
Section 5.3.1 Navigating Arrays 93 is equivalent to a traversal using a pointer: v oi d f p(c r v vo id fp ch ar v[]) { f or (c r* p = v *p 0; p fo r ch ar v; p!=0 p++) u se p); us e(*p } The prefix * operator dereferences a pointer so that *p is the character pointed to by p p 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 §5.9[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 T*, assumed to point to an element of an array of objects of type T p points to the next element of T; p+1 that array, and p points to the previous element This implies that the integer value of p will p-1 p+1 be s iz eo f(T larger than the integer value of p For example, executing si ze of T) p #i nc lu de in cl ud e io st re am i nt m n () in t ma in { i nt v i[1 0]; in t vi 10 s ho rt v s[1 0]; sh or t vs 10 s td :c ou t