ECS 30 Lecture Notes - Lecture 14: Whitespace Character, Null Character, Memory Address
Wednesday, 2 May 2018
ECS 030 Lecture 14
Strings
Arrays vs Strings
-Think of strings as a quote
•can put anything in in it (any
character)
•can only occur as an
element of an array
-Last byte of array of characters
must be 0 to be considered as
a string
-Placeholder %s for printing a
string
Initialisation
-String must be one
"more" than the
number of characters,
for the ending null
character:
-Strings can be smaller than an array, but they cannot be bigger than an array
•if the “quote” has 21 characters, and it’s placed at index 20, the last index in the
array, error will occur
1
Document Summary
Think of strings as a quote: can put anything in in it (any character, can only occur as an element of an array. Last byte of array of characters must be 0 to be considered as a string. more than the number of characters, for the ending null character: Strings can be smaller than an array, but they cannot be bigger than an array: if the quote has 21 characters, and it"s placed at index 20, the last index in the array, error will occur. String variable passed to scanf() is a memory address: arrays always decay into pointers, no need for & Scanf() comes with some speci cities and limitations around strings: has limitations. Skips leading whitespace before the input (whitespace, tabs, newlines) Stops matching a string at the rst whitespace character: if you want to input more separate words into a string, you will need to use multiple scanf statements.