EECS 280 Lecture Notes - Lecture 5: C String Handling, String Literal, Null Character

99 views2 pages

Document Summary

C-style strings: strings used to be represented as an array of characters. Char str(cid:1005)[6] = { (cid:858)h(cid:859), (cid:858)e(cid:859), (cid:858)l(cid:859), (cid:858)l(cid:859), (cid:858)o(cid:859), (cid:858)\(cid:1004)(cid:859) }; Compiler automatically puts /0 at the end of string literals: there is a null character at the end of every string to say stop here (sentinel, they are char arrays. Char values are just numbers underneath: character arrays turn into pointers as well. Be careful with c-style strings: do(cid:374)(cid:859)t (cid:272)o(cid:373)pare the(cid:373) with == because it tests if at the same address, str(cid:1005)=str(cid:1007) does(cid:374)(cid:859)t (cid:272)o(cid:373)pile due to type (cid:373)is(cid:373)at(cid:272)h, ptr=str3 makes ptr pointer to different string. With prior declaration char *ptr = str1. Declaring c-style strings: string literal has to be stored somewhere. In declaring a(cid:374) array, you are (cid:862)spe(cid:272)ifyi(cid:374)g(cid:863) where. In declaring a pointer, compiler gives you pointer to string literal. Traverse c-style string: keep going until sentinel is found. While (cid:894)*ptr != (cid:858)\(cid:1004)(cid:859)(cid:895){ return ptr str;

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents