Presentation is loading. Please wait.

Presentation is loading. Please wait.

COP 3275 – Character Strings and Introduction to Pointers Instructor: Diego Rivera-Gutierrez.

Similar presentations


Presentation on theme: "COP 3275 – Character Strings and Introduction to Pointers Instructor: Diego Rivera-Gutierrez."— Presentation transcript:

1 COP 3275 – Character Strings and Introduction to Pointers Instructor: Diego Rivera-Gutierrez

2 Administrative stuff Did you guys have a good break? Quiz #5 was due 2 minutes ago! (give or take) Friday (7/3) is the observed day for Independence Day – No class No quiz this week

3 Administrative stuff Homework #4 is due Thursday. How is that going? Who has finished the interactive portion? (Receiving plays) Who has finished coding the losing condition? Who has finished the open procedure? Anyone has worked on the extra credit?

4 Quick overview of last class Character strings store text! char * ; //pointer char [ ]; //array char *str = "this works fine"; printf("%s", str); Console arguments int main (int argc, char **argv); //more standard int main (int argc, char *argv[]); //easier to understand

5 Length of a string int length(char *s) { int len = 0; while(s[len] != '\0') { len++; } return len; }

6 Length of a string int length(char *s) { int len = 0; while(s[len]) { len++; } return len; } Slightly more efficient!

7 User string input Let’s say we have a piece of code like this one: char *str; scanf("%s", str); //notice that this one doesn’t need & printf("%s", str); What do you think will happen? Let’s test it

8 User string input Ok, so we need to allocate the string. char str[100]; //using array notation makes sense here. scanf("%s", str); //notice that this one doesn’t need & printf("%s", str); What happens when we try to read more than one string?

9 User string input char s1[100]; //using array notation makes sense here. char s2[100]; //using array notation makes sense here. scanf("%s%s", s1, s2); //still we don’t use & for strings printf("%s\n", s1); printf("%s\n", s2);

10 Comparing strings In the previous one. How do I know if the two strings are the same? For example, the user inputs: Independence Could I compare if(s1 == s2) Why or why not?

11 Comparing pointer types In general, == compares values. “Values” when it comes to pointers refer to positions in memory So when I do s1 == s2, the code I’m generating is: Are s1 and s2 pointing to the same location in memory? NOT: Are s1 and s2 storing the same value. This is the same for arrays and pointers (wait for it…)

12 Comparing pointer types So if I do: int arr1[2] = {0,1}; int arr2[2] = {0,1}; The same problem arises.

13 Comparing strings int equal(char *s1, char *s2) { int i = 0; while(s1[i] != '\0' && s2[i] != '\0' && s1[i] == s2[i] ) { i++; } return s1[i] == '\0' && s2[i] == '\0'; }

14 Comparing strings int equal(char *s1, char *s2) { int i = 0; while(s1[i] &&s2[i] && s1[i] == s2[i] ) { i++; } return s1[i] == '\0' && s2[i] == '\0'; }

15 Comparing strings int equal(char *s1, char *s2) { int i = 0; while(s1[i] &&s2[i] && s1[i] == s2[i] ) { i++; } return !(s1[i] || s2[i]); } Isn’t this one beautiful???? OK, OK, maybe it’s just me

16 Comparing strings While understanding those functions is important to our understanding of strings. Truth is we rarely have to write that particular function (or the length one for that matter). defines a lot of useful functions. strcmp – compares two strings (and does more that our equal function) strcmp strlen – finds the length of a string. strlen

17 Comparing structs? This produces a similar problem. However, this is not allowed in compilation. == as an operator, just doesn’t know how to compare non-standard types. To compare structs, you need to define functions.

18 Pointers

19 Ok… so what’s a pointer? Let’s first talk about “indirection”


Download ppt "COP 3275 – Character Strings and Introduction to Pointers Instructor: Diego Rivera-Gutierrez."

Similar presentations


Ads by Google