C++ Chapter 15 C-Strings & 2D Arrays PDF

Title C++ Chapter 15 C-Strings & 2D Arrays
Course C++ Programming 1
Institution Orange Coast College
Pages 2
File Size 78.8 KB
File Type PDF
Total Downloads 64
Total Views 149

Summary

Notes to the introduction of C-strings and how to use them. 2d arrays are introduced in this chapter as well . C++ chapter 15 notes...


Description

C++ Ch 15 C-Strings & 2D Arrays What are C-strings ? ● C-strings are built into the c++ language ● C-strings are char arrays with the NUL char ‘\0’ ● C-strings can be passed to functions without overhead ● Char arrays have an extra character, the extra being ‘\0’ ● For example “hello, world” has 3 characters, 12 are meaningful characters and the extra is the terminating NUL ‘\0’

Pointer C-strings ● Pointers to NUL-terminated character array literals can be used as C-strings ● Ex) const char *s3 = “String #3”; ● The character array is stored in the static storage area when the program loads

Using C-strings ● You cannot assign, compare or concatenate c- strings in the same way with the string class ● To assign c-strings use : strcpy(dest, src) ○ strncpy() is safer to use than strcpy() ○ strncpy() function copies a specified number of characters ○ You call stncpy() with a dest, src, and count of characters ○ Ex) strncpy( dest, src , MAX_LEN - 1 ); ● To concatenate c-strings use : strcat(dest, src) ○ strncat() is safer to use than strcat() ○ If used incorrectly it can overflow like strcat() ○ Prototype = char * strncat(char *dest, const char *src, size_t count); ○ Count is the maximum number of characters to be copied ● To compare c-strings use : strcmp(cstr1, cstr2) ○ This compares cstr1 and cstr2 lexicographically and returns an integer showing their relationship ○ Zero is the two strings are equal ○ Negative if the first string precede the second string

○ Positive if the first string follows the second string

2D -Array & Friends ● A 2D array is a block of memory that with visualization it has rows and columns ● Syntax = type arrayName[rows][columns] ○ Ex) int arr1 [2][3] ● To access an element use two subscripts. The first subscript is the row and the second is the column ● Both subscripts start at 0 ● Physically the elements in a 2d array are store linearly ● A 2d array offset expressions means : *(address + (row * row - width + col) ) ● Initializing a 2d array ○ It can be declared in a list like this : ○ Int a2d[2][3] = { {5,19,3}, {22,-8, 10} }; ○ Each row has its own set of curly braces ○ But you can remove the curly braces and write like this : int a2d[2][3] = { 5,19,3, 22,-8, 10 }; ● Partial initialization ○ You can partially fill a array but those elements with no value will be set to zero ○ For example = int a2d[2][3] = { {5}, {22, -8} }; = there will be two zeros in the first row ○ It is possible to remove the row dimension but the column dimension is required ○ For example = int a2d [ ] [ 3 ] =; ● 2D arrays and Function ○ 2d arrays are passed to functions by address like 1d arrays ○ Ex) void print (const double m[ROWS][COLS])...


Similar Free PDFs