Download presentation
Presentation is loading. Please wait.
Published byHector Bates Modified over 9 years ago
1
COP 3275 – Character Strings Instructor: Diego Rivera-Gutierrez
2
Administrative stuff Next week is break week! (From M 22 nd to F 26 th ) Hope you guys have a good break! Quiz #5 is posted. Due Monday 29 th before class. LATE POLICY DOES NOT APPLY TO THIS QUIZ If you haven’t submitted by Canvas deadline, you get a 0. No excuses. Homework #4 was fixed (error regarding opening) The solutions of HW #2 and #3 were posted You are free to start HW#4 based on my solution.
3
O (open a tile) O: Open the tile. If it is an empty space…. All adjacent tiles that are not mines get open too.
4
Character Strings They store text! In essence they are character arrays. We have been using strings a lot! All of our printfs and scanfs included strings in one way or another.
5
Character Strings Type? char * ; //pointer char [ ]; //array Literals? "Anything inside double quotation marks" Printf? printf("%s", "this works fine"); char *str = "this works fine"; printf("%s", str); printf(str); //this is a warning, but actually works
6
Dealing with variable sizes One of the advantages of the pointer (char *)notation: We don’t have to worry about size. If we use array (char []) we can keep the length in a separate int. We can always declare functions that always receive the length. However, it becomes tedious to keep track of length for each string. The trick is “null termination” ('\0'). But… why do we need it?
7
"Break" char string[5] = {'B','r','e','a','k'}; Break string I need to keep track of the size (5). What happens if I want to store “Case” or “a” in string? What if it was “Summer”?
8
"Break" char string[6] = {'B','r','e','a','k','\0'}; Break string \0
9
"Break" char string[6] = {'B','r','e','a','k','\0'}; string = "Case"; Break string \0Creak string \0Caeak string \0Casek string \0Case string \0 No longer need to keep track of the 6, right?
10
"Break" char string[6] = {'B','r','e','a','k','\0'}; string = "Case"; string = "a"; Case\0 string \0aase string \0a se string \0 No need to clean the rest, as soon as we hit \0, we know it’s the end. What if it was “Summer”? Well that’s why the pointer notation is useful
11
Null termination Lets us know where a string ends No need to explicitly keep track of size (on most cases) It’s the default if we use the literals: char *string = "Break"; Break string \0 Potentially allocates more spaces than needed (depending on code)
12
Accessing characters Similar to arrays we can access characters using []. So for the previous example: char *string = "Break"; char c = string[0]; //that accesses the 'B' char d = string[5]; //that accesses the '\0'
13
Length of a string int length(char *s) { int len = 0; while(s[len] != '\0') { len++; } return len; }
14
Length of a string int length(char *s) { int len = 0; while(s[len]) { len++; } return len; } Slightly more efficient!
15
Now… console parameters Have you noticed that we do: $ gcc HW3.c –o HW Turns out gcc, is a compiler written in C! It’s complicated! Take a compilers class or read about bootstrapping if you want to know how that works. How does it read the file name for our code from console? How does it read the output file?
16
Console parameters We have been purposedly ignoring any parameters that get sent to main! That’s why we write: int main ( void ); I am specific about doing that, because it makes the ignoring of those parameters very explicit. However, main can have parameters! It actually has 2 parameters!
17
Console parameters The common way to write it is: int main (int argc, char **argv); //more standard int main (int argc, char *argv[]); //easier to understand argc says how many parameters there are argv is an array of parameters. The parameters we get are separated by spaces. So if I type: $ gcc HW3.c –o HW What happens?
18
Console parameters gcc HW3.c –o HW argc would be ? argv would be: gccHW3.c-oHW argv
19
Console parameters gcc HW3.c –o HW argc would be 4 argv would be: gccHW3.c-oHW argv
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.