Strings and Introduction to Pointers Instructor Diego RiveraGutierrez Administrative stuff Did you guys have a good break Quiz 5 was due 2 minutes ago give or take Friday 73 is the observed day for Independence Day ID: 570083
Download Presentation The PPT/PDF document "COP 3275 – Character" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
COP 3275 – Character Strings and Introduction to Pointers
Instructor: Diego Rivera-GutierrezSlide2
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 weekSlide3
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?Slide4
Quick overview of last class
Character strings store
text
!
char *<identifier>;
//pointer
char <identifier>[<max-size>];
//
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 understandSlide5
Length of a string
int
length
(
char
*s
)
{
int
len
= 0;
while
(
s[
len
]
!= '\0'
) {
len
++;
}
return
len
;
}Slide6
Length of a string
int
length
(
char
*s
)
{
int len = 0; while(s[len]) { len++; } return len;}
Slightly more efficient!Slide7
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 itSlide8
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?Slide9
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 stringsprintf("%s\n", s1);printf("%s\n", s2);Slide10
Comparing strings
In the previous one. How do I know if the two strings are the same?
For example, the user inputs:
Independence
Independence
Could I compare
if(s1 == s2)
Why or why not?Slide11
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…)Slide12
Comparing pointer types
So if I do:
int
arr1[2] = {0,1};
int
arr2[2] = {0,1};
The same problem arises. Slide13
Comparing strings
int
equal
(
char
*
s1,
c
h
ar *s2) { int i = 0; while(s1[i] != '\0' && s2[i
]
!= '\
0' &&
s1[
i
] == s2[
i
]
)
{
i
++;
}
return
s1[
i
] == '\0' && s2[
i
]
== '\0'
;
}Slide14
Comparing strings
int
equal
(
char
*
s1,
c
h
ar *s2) { int i = 0; while(s1[i] && s2[i] && s1[i
] == s2[
i
]
)
{
i
++;
}
return
s1[
i
] == '\0' && s2[
i
]
== '\0'
;
}Slide15
Comparing strings
int
equal
(
char
*
s1,
c
h
ar *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 meSlide16
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).
<
string.h
> defines a lot of useful functions.
strcmp
– compares two strings (and does more that our equal function)
strlen
– finds the length of a string.Slide17
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.Slide18
PointersSlide19
Ok… so what’s a pointer?
Let’s first talk about
“
indirection”