<NOSCRIPT><A href="http://about.com/" target=_blank><IMG src="C++ Tutorial - Lesson 11 Strings_files/0.gif" border=0></A></NOSCRIPT>   About > Computing & Technology > C/C++ 
Search     
 Hi, I'm John Kopp, your guide to C, C++ and C# programming. This site provides what you need to know to learn C, C++ or C# programming. Browse the subjects on the left, try one of the tutorials or use the search box to get started.

C/C++

with John Kopp
Your Guide to one of hundreds of sites
 Home · Articles · Forums · Chat · Newsletters · Help    
Subjects

  ESSENTIALS
· C Tutorial
· C++ Tutorial
· C/C++ Glossary
· Tips
· More Tutorials
  BUYER'S GUIDE

Product Reviews
Top Picks
C Tutorials
C++ Tutorials
C#
Advanced C
Advanced C++
Beginning C
Beginning C++
Books
C++ Builder
Careers
CGI
CM
Dictionaries
CompilingDebugging
Freeware/Shareware
Humor
Magazines
OOAD
Polls
SoftwareEngineer'n
Style
STL
UNIX/GNU
Visual C++

Subject Library

All articles on this topic

 

Stay up-to-date!
Subscribe to our newsletter.

Web Hosting
Global Servers

 
 
 
Advertisement
> Free Credit Report
 
C++ Tutorial - Lesson 11: Strings
 More of this Feature
• C-Style Character Arrays
• String Class
• String Operations
• String Class Methods
• Using String Methods
 
 Related Resources
• C++ Tutorial: Input and Output
• C++ Programming Tips on Input
• C Tutorial: Strings
• Advanced C++ Tutorials
• Simple Solutions in C++
• C++ Programming Tips
• All C++ Lessons
 

by John Kopp

Welcome to About.com's tutorial on C++ programming. This lesson covers stings. C++ has two methods for representing strings, C-style character arrays and the string class. C-style character arrays are a low-level, primitive representation of string data. Although the string class provides more functionality and is less error prone in use, it is not uncommon to see C-style character arrays in C++ code, so it is important to understand and be able to use this representation. As will be seen in a later lesson, this representation is also used in C++ code to handle command line arguments. The string class, which is part of the C++ standard library, provides methods for easy manipulation of string data. Additional functionality is added by the generic algorithms introduced earlier in this tutorial.

C-Style Character Arrays
As implied by its name, C-style character arrays is the representation of string data used in the C programming language. In C, this is the only technique for storing and manipulating string data. Stings are stored as null, '\0', terminated character arrays. This representation has several weaknesses and should generally be avoided in C++ programming in favor of the use of the string class.

  • A character array of sufficient size must be defined or allocated to hold the string. The array must be at least the length of the string plus one. One byte is needed to hold the null terminator. It is the programmer's responsibility to be certain that the array is large enough. The compile will not issue any warnings or errors if they size is too small. Errors in array size will result in run-time errors. The program may crash, behave erratically or operate incorrectly.
  • At times, it is necessary to explicitly add the null terminator.
  • Pointers are commonly needed and used to access and manipulate the string data.
  • When copying strings, the programmer must check that the destination array is large enough. When adding to strings, again, the array size must be considered.

WAIT. Before you skip immediately to the next section, there are several important reasons to study and learn about C-style strings. First, if you need to maintain any legacy C++ code as part of a job, you will encounter C-style strings. Some programmers do use them. Additionally, the string class was not part of the library of early versions of C++. Programmers typically built their own string classes or used C-style strings. In either case, knowledge of C-style strings in necessary. The final reason to learn this representation is that it is used in C++ to handle command line arguments. Command line arguments are data or instructions that are passed into a program when it begins execution. These arguments are passed into a C++ program as C-style character arrays. Command line arguments will be covered in detail in a later lesson.

The C tutorial contains a lesson that describes the use of C-style strings. I recommend reading this lesson to understand C-style strings. C++ is a superset of C. That is, it contains all of the C language plus much more. A valid C program is also a valid C++ program. Everything in the C lesson will be understandable from what you've learned in the C++ tutorial.

Next page > The String Class > Page 1, 2, 3, 4, 5


Subscribe to the C/C++ Newsletter
Name
Email






Email this page | Sign up for a Newsletter |
Explore More
Most Popular Articles
• C++ Programming Tutorial
• C Programming Tutorial
• C++ Tutorial - Lesson 12: File Input and Output
• C++ Tutorial - Lesson 4: Input and Output - cin, cout, iostr...
• C++ Tutorial - Lesson 10: Arrays and Vectors
• C++ Tutorial - Lesson 1: Writing and Compiling A First Progr...
• Tutorials
What's Hot Now
• Teach Yourself C++ in 21 Days by Jesse Liberty
• C Programming Tutorial
• C++ Tutorial - Lesson 25: Operator Overloading
• The C Programming Language, 2nd Edition by Kernighan and Rit...
• Topics in C++: Function Template Specialization
• C Programming Tips: Using Long Constants
• C Tutorial - Lesson 5: Conditional Processing, Part 1 - Rela...
About Us | Advertise on This Site | User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2004 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.