CSE1320Ch5Notes - Ch. 5 Notes - Strings I. A string in 'C'...

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

View Full Document Right Arrow Icon
Ch. 5 Notes -- Strings I. A string in 'C' is a continuous sequence of characters terminated by a null character, '\0' (“backslash zero”). A. The \0 must be there to terminate the string. B. The \0 may be put there by a pre-defined function, or by the programmer's code. II. Rules A. Constant strings are enclosed in double quotes, as in printf("Testing, 1, 2, 3\n"); where Testing, 1, 2, 3\n is stored as 18 characters: Testing, 1, 2, 3\n\0 and where each character (T, E, \n, \0, etc.) is defined in 1 byte or 8 binary bits of information. B. Printout of a string terminates with the last character before the '\0'. C. A string is stored just like a character array, except for the additional null character. D. ex. 5-1, p. 259 -- how C compilers handle strings main() { char *stringptr; stringptr = “Testing, 1, 2, 3\n”; printf(stringptr); printf(“%s”, stringptr); printf(“%p\n”, stringptr); printf(“First character: %c\n”, stringptr[0]); printf(“Second character: %c\n”, *(stringptr + 1)); } E. ex. 5-2, p. 261 -- using arrays for strings main() { char work_string[512] = “One two, buckle my shoe\n”; printf(work_string); printf(“%s”, work_string); printf(“%p\n”, work_string); *(work_string + 7) = ‘\n’; work_string[8] = ‘\0’; printf(work_string); 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
} F. Access of individual characters can be done either by pointer arithmetic char *string_ptr or array subscripting notation str_array[4] G. However, an error can occur if an attempt is made to append new characters to the end of a string which would go beyond the bounds of the character array. H. If a program needs to process strings of unknown or changing length, enough space
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/14/2008 for the course CSE 1320 taught by Professor Sprintston during the Spring '08 term at UT Arlington.

Page1 / 6

CSE1320Ch5Notes - Ch. 5 Notes - Strings I. A string in 'C'...

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

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