{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture35 C-Strings and Strings

lecture35 C-Strings and Strings - CMPT 128 Introduction to...

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

View Full Document Right Arrow Icon
1 © Janice Regan, CMPT 128, February. 2007 CMPT 128: Introduction to Computing Science for Engineering Students C-strings and strings
Background image of page 1

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

View Full Document Right Arrow Icon
2 Simple and Composite Variables We have studied simple variables A simple variable describes a single value A simple variable has an identifier A simple variable has a type that describes the properties of the value of the variable, the permissible operations for the variable, and the representation of the variable in computer memory We can also have composite variables These variables describe a group of values Arrays: all values in the group have the same type Structures: different values in the group can have different types
Background image of page 2
3 Composite Variables composite variables describe a group of values 1 dimensional arrays or variables of a particular type (all entries must have the same type) multi dimensional arrays or variables of a particular type (all entries must have the same type) Structures containing groups of variables of different types Strings are another special type that builds on arrays An array of characters A set of special operations appropriate for text
Background image of page 3

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

View Full Document Right Arrow Icon
4 One-Dimensional (1-D) Arrays An array is an indexed data structure All variables stored in an array are of the same data type An element of an array is accessed using the array name and an index or subscript The name of the array is the address of the first element and the subscript is the offset In C++, the subscripts always start with 0 and increment by 1
Background image of page 4
5 Example String Declaration in C char list[10]; allocates memory for 10 characters. Ten adjacent locations in memory are allocated Remember C++ does not perform any bounds checking on arrays list[0] list[1] list[2] list[3] list[4] list[5] list[6] list[7] list[9] list[8]
Background image of page 5

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

View Full Document Right Arrow Icon
6 Initializing 1-D Arrays Strings are not the same as 1-D character arrays You can specify individual values for each character in a 1-D character array /* put one character in each element of the array*/ char list[8] = {‘h’,’e’,’l’,’l’, ‘o’}; After initialization memory looks like list[0] ‘h’ list[1] ‘e’ list[2] ‘l’ list[3] ‘l’ list[4] ‘o’ list[5] \0 list[6] \0 list[7] \0 list[8] \0
Background image of page 6
7 Difference: string vs 1D character array string is a C++ class that includes operators C-strings are partially filled character arrays that are used in a particular way in C++ or in C (where there is no direct support for classes, so no overloading of operators) A C-string always ends with a null termination character (\0) This tells all the functions in the string library where the string ends, allowing manipulation of the C-strings Use of the null termination character allows strings of different length to be stored in a character array of a single length
Background image of page 7

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

View Full Document Right Arrow Icon
8 Strings of different lengths Strings of different lengths can be stored in a character array The maximum number of character in the string is the number of characters in the array minus one Blanks can be included in the string Blanks count as characters
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}