cs31s11dis6 - CS31: Introduction to Computer Science I...

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

View Full Document Right Arrow Icon
Discussion 1H Notes (Week 6, May 6) TA: Brian Choi ([email protected]) Section Webpage: http://www.cs.ucla.edu/~schoi/cs31 C Strings C++ has an older brother, whose name is C. In C, we did not have the data type string , and C had its own way of representing strings. C++ inherited this older way of representing strings from C, and these old strings are still widely being used. For this reason, we are teaching you this old scheme, and we will call these strings “C strings.” Recall that a string is a sequence of zero or more characters . Also recall that an array is a sequence of variables of the type you de±ne. Combing the two ideas, we see that we can use an array of characters to represent a string. Let us define a string as follows: char s[10]; This creates space for a string of length 9. Wait, why 9, not 10? Every C string must end with a marker that says “this is the end of the string.” Otherwise, it is nothing but a character array! And this end marker occupies a slot, thus char s[10] can store up to 9 characters and an end marker, totalling 10. This end marker is what we call the zero byte (sometimes called the null character ), which we use an escape sequence \0 to represent (its ASCII number is 0), and we say C strings are null-terminated . The length of the string is determined by the location of the zero byte. One can initialize a string as he/she declares it: char s[10] = "HOWAREYOU"; H O W A R E Y O U \0 This will automatically ±ll in the characters for you and ±ll the last slot with the zero byte. You can also choose not to specify the size:
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 07/06/2011 for the course CS 31 taught by Professor Melkanoff during the Spring '00 term at UCLA.

Page1 / 4

cs31s11dis6 - CS31: Introduction to Computer Science I...

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