CSE100Lab10_ManskiJosh - Lab Section:_ Name:_ Lab 10...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Lab Section:________ Name:_____________________________________ Lab 10 C-Strings Pre-Lab: Please read the pre-lab and answer the accompanying questions before your lab session. In this lab you will (hopefully) learn: what C-strings are and how they differ from the string type how to declare and initialize C-strings how to use C-strings in input and output what the <cstring> library is and how to use it with C-strings Thus far, when we've needed to handle text in our programs we have used the string type. This type has been very useful to us, because it automatically takes care of things like changing size as necessary, comparing strings of text to other strings, and integrating smoothly with input and output. Now come with me children as we take a step back in time to the wild days of C, where void pointers ruled the land and object-oriented programming was something those weird LISP programmers did. Back then, strings (as we know them) didn't exist, because C couldn't handle the back-end code required to implement such a thing. Nonetheless, programmers still needed to be able to work with strings of text, so they created the C-string. Basically, C-strings are just char arrays with some special (non-binding) rules on what can go in them. Other than a library of a few functions that add some basic functionality to them, that's it. "Wait a minute," you must be asking, "why are we learning about something that's 20 years old and doesn't work as well as what we've been using?" That's a good question. The real answer is a little cryptic and hard to understand, so for now it's better just say that C-strings are useful because they allow us to interact with older code libraries and they give us a healthy appreciation for the wonderful string type C++ gives us. C-strings can do just about everything a string can, just in a different way. For starters, C-strings are declared differently. When you create a C-string, think of the maximum number of characters (i.e., letter, numbers, etc.) you'd like to store, and create a char array one bigger than that. So, if we wanted to be able to store the word "puppy", we'd declare the C-string like this: char cuteAnimal[6] = {'p', 'u', 'p', 'p', 'y', '\0'}; This creates an array of six characters, the first five of which make the word "puppy". Right now you're probably asking, "What's that slash zero thing doing there?" Well, that's the null character . It literally means "nothing". Since C-strings are just character arrays at heart, we need to somehow tell C++ when the string ends. No, we can't just use the size of the array (what if we wanted to store "cat" in that array? What would the last two characters be?), and besides C++ doesn't even know how big our arrays are once it starts running the program (stupid, I know, but we have to deal with it). So, lacking any other cool ideas, we use the null character to tell C++ that our C-string is done. It's very important that every C-string we make ends with at least one null character, or else very strange things will get output to the console when we start
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/20/2010 for the course CSE 100 taught by Professor Na during the Spring '09 term at Arizona.

Page1 / 6

CSE100Lab10_ManskiJosh - Lab Section:_ Name:_ Lab 10...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online