tailieunhanh - A Complete Guide to Programming in C++ part 38
A Complete Guide to Programming in C++ part 38. 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. | chapter 17 Arrays and Pointers This chapter describes the relationship between pointers and includes pointer arithmetic pointer version of functions pointers as return values and read-only pointers pointer arrays Operations that use C strings illustrate how to use pointers for efficient programming. String access via the command line of an application program is used to illustrate pointer arrays. 349 350 CHAPTER 17 ARRAYS AND POINTERS ARRAYS AND POINTERS 1 Sample program Using arrays of char and pointers to char -------------------------------------------------------- include iostream using namespace std int main cout Demonstrating arrays of char and pointers to char. n endl char text Good morning name Bill char cPtr Hello Let cPtr point to Hello . cout cPtr name n text endl cout The text text starts at address void text endl cout text 6 What happens now endl cPtr name Let cPtr point to name . cPtr is equivalent to name 0 cout This is the cPtr of cPtr endl cPtr k cout Bill can not cPtr n endl return 0 Sample output Demonstrating arrays of char and pointers to char. Hello Bill Good morning The text Good morning starts at address 00451E40 morning This is the B of Bill Bill can not kill ARRAYS AND POINTERS I 351 Name and Address of an Array In C the name of an array is also the starting address for that array. To be more precise an array name is a pointer to the first array element. Example char town Beijing In this case town is a char pointer to town 0 that is a pointer to the memory address that stores the B character. Expressions town and town 0 are thus equivalent. Example cout town or cout town 0 A pointer to the first character of the string town is passed. The characters forming the string are read and displayed from this point onward until the terminating null character 0 is reached. Pointer Variables and Arrays An array name is not a pointer variable but a constant that cannot be modified. However you can assign this constant to a .
đang nạp các trang xem trước