Absolute C++ (4th Edition) part 36

Absolute C++ (4th Edition) part 36. KEY BENEFIT: C++ programming concepts and techniques are presented in a straightforward style using understandable language and code. KEY TOPICS: C++ Basics; Flow of Control; Function Basics; Parameters and Overloading; Arrays; Structures and Classes; Constructors; Operator Overloading, Friends, and References; Strings; Pointers and Dynamic Arrays; Separate Compilation and Namespaces; Streams and File I/O; Recursion; Inheritance; Polymorphism and Virtual Functions; Templates; Linked Data Structures; Exception Handling; Standard Template Library; Patterns and UML. MARKET: Useful for both beginning and intermediate C++ programmers. . | An Array Type for Strings 355 Pitfall The cstring Library You do not need any include directive or using statement to declare and initialize C-strings. However when processing C-strings you inevitably will use some of the predefined string functions in the library cstring . Thus when using C-strings you will normally give the following include directive near the beginning of the file containing your code include cstring The definitions in cstring are placed in the global namespace not in the std namespace and so no using statement is required. Using and with C-strings C-string values and C-string variables are not like values and variables of other data types and many of the usual operations do not work for C-strings. You cannot use a C-string variable in an assignment statement using . If you use to test C-strings for equality you will not get the result you expect. The reason for these problems is that C-strings and C-string variables are arrays. Assigning a value to a C-string variable is not as simple as it is for other kinds of variables. The following is illegal assigning a C-string value char aString 10 aString Hello Illegal Although you can use the equal sign to assign a value to a C-string variable when the variable is declared you cannot do it anywhere else in your program. Technically the use of the equal sign in a declaration as in char happyString 7 DoBeDo is an initialization not an assignment. If you want to assign a value to a C-string variable you must do something else. There are a number of different ways to assign a value to a C-string variable. The easiest way is to use the predefined function strcpy as shown below strcpy aString Hello This will set the value of aString equal to Hello . Unfortunately this version of the function strcpy does not check to make sure the copying does not exceed the size of the string variable that is the first argument. Many but not all versions of C also have a version of strcpy that takes a third argument which .

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
Đã 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.