Operating Systems CS 3100
Weber State University
Popular in Course
Popular in ComputerScienence
This 7 page Class Notes was uploaded by Jamaal Franecki on Wednesday October 28, 2015. The Class Notes belongs to CS 3100 at Weber State University taught by Staff in Fall. Since its upload, it has received 18 views. For similar materials see /class/230778/cs-3100-weber-state-university in ComputerScienence at Weber State University.
Reviews for Operating Systems
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 10/28/15
CStrings A Review QM CStrings Two characterstring representations I Nullterminated singledimensioned character array gt Array must be 1 character longer than the longest string that it holds gt Compiler automatically inserts the null at the end of string constants gt The array name without brackets is a constant character pointer I Character pointer gt Must point to acharacterarray S1Examp e O I a string constant char s1 100 quotExamplequot 52 char 2 s1 s3 EIIII QM char s3 quotHelloquot An Alternate View of CStrings Focusing on addresses char string8 quotEXAMPLEquot char strptr string array name is an address cout ltlt string ltlt ltlt strptr ltlt endl cout ltlt strlenstring ltlt ltlt strlenstrptr ltlt endl 100101102103104105106107 string strptr contains the address of the first strptr 108 character in string String Functions ANSI Cstring library functions I Prototypes are in ltcstringgt or ltstringhgt I Most useful functions gt int strlenchar s length does not include null gt int strcmpchar 51 char 52 returns lt 0 0 gt 0 l gt char strcpychar 31 char 32 s1 4 52 gt char strdupchar s duplicates s returns pointer gt char strcatchar 31 char 2 appends 2 at the end of s1 gt char strchrchar 5 char c search for char c in string 5 gt char strstrchar 31 char 2 search for string 52 in string s1 gt char strtokchar s1 char s2 tokenize s1 delimiters in 2 QM strlen and strcpy Examples Counting and copying characters array 01234567 char array8 quotEXAMPLEquot cout ltlt strlenarray ltlt endl char s1 50 quotmystuffquot char 250 quotHelloquot K39 strcpys1 2 HeIIoo 32 The quot1 quot left in s1 is ignored Concatenating Strings The strcat function char s1100 quotHelloquot char sZ100 quotworldquot strcats1 52 0123456789 HeIIIo0 31 Comparing Strings Are two strings equal I int strcmpchar 31 char 2 gt returns a value lt 0 if 51 is ordered before 32 gt returns 0 if s1 and s2 are identical gt returns a value gt 0 if 51 is ordered after 2 gt is casesensitive gt short strings sort before long strings strcmpquotaaquot quotaaaquot lt 0 strcmpquothelloquot quotworldquot is 15 104 39h 119 39w 15 strcmpquotworldquot quothelloquot is 15 119 39w 104 39h 15 strcmpquothello quot quothello quot is 0 h h 0 e39 e 0 II II ll II lo lo 0 QM CString Searching Functions strchr strstr strrchr and strrstr char s1 strchrs 39e39 char s3 strstrs quotworld quot char s2 strchrs 39a39 char s4 strstrs quotfred quot s2 is NULL not found S4 is NULL not found cout ltlt s1 ltlt endl prints ello world cout ltlt s3 ltlt endl prints world I strchr and strstr search left to right I strrchr and strrstr search right to left QM Parsing with strtok IA token is one or more characters that have meaning ITokens are separated by delimiters in strings Istrtok inserts nullcharacters in the string it parses char s100 See the quick red foxquot char t strtoks tnquot there is a space before t while t NULL cout lt t ltlt endl t strtokNULL tnquot t m See the quick red ox 3 3 ll 3 QM Pointer Arithmetic Addition subtraction and comparison IAssumes a contiguous block of memory ie an array IAllowed operations gt Addsubtract an integer tolfrom a pointer p1 i i p2 gt Difference between two pointers p2 p1 i of elements gt Test for inequaity if p1 p2 if p1 p2 I No check for addressing out of bounds IThe compiler automatically multiplies and divides by the size in bytes of the referenced object in pointer arithmetic p5 p2 p1 p p1p2 p5 Ip239lp14 p1 p2 QM String IIO Reading and writing cstring values include ltiostreamgt using namespace std int main const int cin gtgt input cout ltlt input ltlt endl cingetineinput MAX cout ltlt input ltlt endl cingetinput MAX cout ltlt input ltlt endl return 0 MAX 100 char inputMAX doesn t read spaces discards n leaves n in the input stream 9 CommandLine Arguments Array of cstrings see pp 622 624 int main int argc char argv cgt argdemo hello world from 081220 WN O for i 1 i lt argc i processargvi for i 1 argvi NULL i processargvi for i 1 argvii i processargvi Local Variables Memory allocation and deallocation int wsh nextcommand ERROR Memory is char commandline MAXPATH deallocated when the function returns int wsh nextcommand OKAY static char commandline MAXPATH Memory IS 0t deallocated class wsh OKAY Memory is not private39 deallocated char commandline MAXPATH 92W
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'