TAILIEUCHUNG - A Complete Guide to Programming in C++ part 39

A Complete Guide to Programming in C++ part 39. This book provides both novice and experienced programmers with a comprehensive resource manual for the C++ programming language. Readers gain experience in all aspects of programming, from elementary language concepts to professional software development, with in depth coverage of all the language elements en route. These elements are carefully ordered to help the reader create useful programs every step of the way. | POINTER VERSIONS OF FUNCTIONS 359 Using Pointers Instead of Indices As we have already seen a parameter for an array argument is always a pointer to the first array element. When declaring parameters for a given type T T name is always equivalent to T name. So far in previous sample functions the pointer has been used like a fixed base address for the array with an index being used to access the individual array elements. However it is possible to use pointers instead of indices. Example A new version of the standard function strlen int strlen char str Computes length char p str of str without 0 for p str p return p - str 0 p Search for 0 In this case the difference between two pointers results in the string length. The Sample Functions Opposite The first version of the function strcpy string copy opposite uses an index whereas the second does not. Both versions produce the same results the string s2 is copied to si. When you call the function you must ensure that the char array referenced by si is large enough. As the parameters si and s2 are pointer variables they can be shifted. The second pointer version of strcpy which is also shown opposite uses this feature although the function interface remains unchanged. Generally pointer versions are preferable to index versions as they are quicker. In an expression such as si i the values of the variables si and i are read and added to compute the address of the current object whereas si in the pointer version already contains the required address. Multidimensional Arrays as Parameters In a parameter declaration for multidimensional arrays you need to state every dimension with the exception of the first. Thus a parameter declaration for a two-dimensional array will always contain the number of columns. Example long func int num i0 ok. long func int num i0 also ok. 360 CHAPTER 17 ARRAYS AND POINTERS READ-ONLY POINTERS Sample program Defines and calls a function which outputs a list of overdrawn accounts.

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã 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.