« first day (38 days earlier)      last day (147 days later) » 
02:00 - 04:0004:00 - 06:00

4:00 AM
Don't worry, I'm here to learn.
please go ahead
 
well do you know what it is?
 
A loop is a block of code which keeps repeating itself until a given condition is false.
 
correct
so the first one while (*dest)
when will that condition - *dest - be false?
 
'\0'
 
When we've hit the end of the string.
 
4:01 AM
@johnathon I am going to punch you in the face
 
Ah, got it
 
@Moshe yes, because the end of the string is a 0
 
First move the the end of the destination string, then keep characters from there on, from the source string.
 
yep.
so what does *dest++ = *src++ mean
 
So i'm not terrible at this, just not terribly motivated either. :-)
@stdOrgnlDave Whoa, this is the serious stuff.
Order of operations and whatnot.
 
4:03 AM
:-P
so take a stab at it
 
Sec, thinking.
 
remember that it's also possible to do *++dest = *++src but it isn't that way for a reason. and if that confuses you forget I said it.
 
No I gotcha.
We are assigning first and then incrementing.
 
The other way increments first and will result in a gap in the string. (I think.)
 
4:04 AM
indeed it will
 
Ok, so first we do *src++
Hrm...
 
you already said it though
we assign then increment
 
ok
Yea, this is a little heavy for me, tbh.
 
increment src, move over char to dest, then increment dest?
 
4:07 AM
let's split it apart then
take the ++'s out
you get *dest = *src
dest++ increments dest, but "resturns" dest before it is incremented
so what happens is,
*dest++ = whatever
is *dest = whatever; dest++;
 
ok
 
get it?
 
So the assignment operator takes precedence over the increment operator.
 
hmmm, well,
the assignment operator is =
 
So it seems.
 
4:09 AM
you mean the dereference operator * ?
 
Right.
Er, excuse me - tired too.
 
you're doing fine
 
The dereference operator takes precedence over the increment operator.
 
so if we were to repeatedly do *dest++ = *src++; what would happen each time? memory is assigned, then the pointers are incremented
 
Right, I see.
Is there a less "hardcore" way to write that?
 
4:10 AM
now the question is why the heck does that work in the middle of a while() statement
 
*dest = *src;
 
this is where a bit of the "pornography" bit comes in
 
dest++; src++;
 
Heh, simple.
 
OK :-P let use have it
Oh
 
4:11 AM
Because any statement that executes in C++
 
you were responding to @johnathon being helpful
 
returns 1
or true
 
nope it doesn't
a = 4; returns 4
 
?
 
he's getting = mixed up with ==
 
4:12 AM
Oh, doesn't make a diff.
 
any boolean test returns true or false
 
but = isn't boolean
 
yes.. it is
 
Ok, to rephrase, statements that don't execute return 0, or false.
No?
 
4:13 AM
lol
 
So if an assignment fails, the loop breaks.
 
what happens if I do, a = b = 4;
 
I know it has to do with that, not getting the semantics of it.
It's going to return 4, which is not 0.
 
no I mean what are the values in a and b
 
4:13 AM
acutally .....
 
and why is my driveway being plowed when there is no snow :-( is the neighbor drunk again?
2
 
You let your drunk neighbors drive snowplows? Haha
@stdOrgnlDave 4, no?
 
correct
 
Ohh!
 
mmhmm?
 
4:15 AM
@Moshe a = b = 4, assigns 4 to b, then b to a ..
 
Got it.
While(1) always runs. while(0) never runs.
 
@Moshe the order of the operations is subtle, but has huge semantic impact
 
correct
while(500) always runs
 
When we hit the end, we add 0, so it's while(0)
 
but it's important you understand this
pretend that there's an extra variable here
 
4:16 AM
Ok...
 
while (b=(*dest++ = *src++))
what will b be equal to?
 
0
 
sorry I edited it
 
i was going to say true
but ... anways
 
Ok, so what is in b, the value of dest/src?
 
4:17 AM
b will end up being '\0'
 
well what's the difference between b = *dest++ = *src++ and a = b = 4?
 
at point in the the while stops
 
grabs a lukewarm water bottle, then remembers that there may be one in the fridge
 
@johnathon SHHH
 
Ah, I see.
there's no actual variable but it's implicit.
 
4:18 AM
indeed
 
Well, the anonymous implicit variable.
 
so while() is actually testing *src
so when *src hits a null terminator (0, false) it will exit
 
Lurkers: You can sound impressive by talking about the "anonymous implicit" variable.
In fact, chat room lurkers are like anonymous and implicit too, haha.
 
@Moshe which goes back to the order of the semantics i was talking about
 
Because you can't see them
 
4:19 AM
heh
 
@stdOrgnlDave Got it.
 
don't worry noone ever comes here
and I am immediately made a liar
 
lol
 
@johnathon Explain please.
 
so finally we return the the final statement! return --dest;
why is this not return dest--; ?
 
4:21 AM
decrement, then return? I'm not sure. The function isn't recursive.
Wait a second...
 
@Moshe to correctly explain, in a conditional test any assignment operation will evaluate to the result of the assignment, in the case of the loop posted, the conditional test that was being performed by the while was testing the value of the resulting assignment of the src to the dest
 
actually I have no idea why it does that
it should have kept a temporary copy of dest and returned that instead :-\
 
It would return the last character in the newly combined string...
 
yeah, we want it to return the original dest
so we need to modify that function with a temporary pointer
char *tmp_dest = dest; at the beginning
then return tmp_dest; at the end
 
mmhum
 
4:23 AM
To Joel's blog!
 
head_ptr, tail_ptr
 
Oh, I see.
Joel explains this.
> What have we done here? At very little extra cost we're returning a pointer to the end of the new, longer string. That way the code that calls this function can decide to append further without rescanning the string:
Compare to this version:
void strcat( char* dest, char* src )
{
     while (*dest) dest++;
     while (*dest++ = *src++);
}
 
but that's not what strcat() does in the C standard library
 
It's a performance things.
 
is joel your professor?
 
4:25 AM
No, haha, I wish!
 
you are emulating the C standard library which says to return the original dest pointer
 
@spolsky, want to come to Brooklyn College next semester?
 
who the f*ck is @spolsky
2
 
Oh, ok, so then yea, we can keep a copy of the pointer I guess.
 
i was wondering the same
 
4:26 AM
Joel Spolsky, cofounder of StackOverflow.
 
it's true that that is handy, but it's not how the C standard library does it
so we gotta use that temporary pointer
 
Well, anyway, I doubt he reads half of the pings he gets.
Ok, moving along.
 
someone should star that
maybe he'll read "who the f*ck is @spolsky" and come in here and say it
anyway
onwards and upwards!
next function
you get one more then you gotta do it on your own, I'm up past my bedtime
 
4:30 AM
Heh, ok, sec...
Ok, strchr it is.
const char* mycstring::mystrchr(const char *str, int c){
if (str=="") return NULL;
else if (str==c) return *str;
else return mystrchr(str+1, c);
}

char* mycstring::mystrchr (char *str, int c){
if (str=="") return NULL;
else if (str[0] == (char) c)
return str;
else return mystrchr(str+1, c);
}
 
let me look up strchr
 
Two versions of the function, not sure what either should be doing.
thanks
 
actually if you understood the last one this should take you about 10 seconds
 
What does this function do?
and what's the diff between the two overloaded signatures?
 
um, it's just for "const correctness"
 
4:34 AM
Oh, ok.
Like political correctness.
 
the function should find the first occurance of char c in string str
but it doesn't
@Moshe no, const correctness is actually important
 
Is const correctness like the points on Whose Line Is It Anyway?
 
man you're up way too late
don't worry about it for now
 
Hah, ok. Let's finish this one up.
 
go back to the function before this one
think about how you could search for a char in a while() loop
wait
yes.
 
4:36 AM
while(*str++ == c)
Er, no something related to that though.
 
hmm, that would assign all of str to be c
 
Edited, Meant double equal, but yea.
 
Oh, ok
 
this needs to be a do while
do { } while(*str)
inside the loop, we need to check if it's equal to c, then increment str
 
4:38 AM
Does it?
 
if it isn't
 
We just want the index.
 
if it is, we break
late
 
No no, you shouldn't have to break.
 
I'm impatient
 
4:39 AM
that sounds overcomplicated.
Heh, sorry.
No worries, thanks for all your help.
 
while ((*src++!=c)&&*src);
what does that do!
 
Makes my head explode.
:P
 
do you know what && is
 
Wait, that's "and"
 
what is !=
 
4:40 AM
not equal
 
correct
 
Oh!
got it
 
explain
sorry I'm telling more than teaching now, not your fault, just tired :-P
 
While the character at *src is not equal to c and we haven't hit the null terminator.
 
correct!
now what are the return conditions of this function though? cplusplus.com/reference/clibrary/cstring/strchr
 
4:42 AM
Um.. sec.
What do you mean?
 
that tells you what the function should return
 
Return Value
A pointer to the first occurrence of character in str.
If the value is not found, the function returns a null pointer.
 
that while loop either stops because it finds what it was looking for or hits 0
 
right, of course.
 
so how to transform what we have into that?
 
4:46 AM
if(!*src) return -1;
return *src;
After your while loop
 
close
firstly, remember we are comparing then incrementing
so src will be +1 from where the loop ended
 
return --*src;
 
nextly, we don't need to dereference src, we need to return src itself
 
So it's just return --src;
 
yep, but there's a bit to put in here
at this point it's worthwhile to just do
src--; if (*src == 0) return 0; return src;
 
4:48 AM
what's the final function look like? I don't like the decrement before the if, kinda scares me a bit. I think it'll break your if(*src==0) check because it'll go back by one
No?
 
ya'll still on strchr ?
 
remember how *str++ in the while() loop tested then incremented?
errr
 
it returns an int.
 
str is still incremented after the end of the loop
 
not a char,or a pointer to a char.
 
4:49 AM
Yea
 
it is 1 past wherever it found a 0 or its match
 
Ok, so...
Oh, right.
So even if it hits zero, it's one past that.
I see.
 
yep
so make up the final function
 
Ok, sec.
@stdOrgnlDave Why return 0 if no match found? Not return -1?
 
because that's what the thing says to return
null pointer = 0, not -1
 
4:54 AM
Oh, lol.
Ok
 
of course there's the corner case where the value they were looking for was 0
so we need to put an if in there for that as well
(if c == *str == 0) return str;
 
@stdOrgnlDave facepalm
const char* mycstring::mystrchr(const char *str, int c){
        while ((*src++!=c)&&*src);
        src--;
        if(*src==0) return 0;
        return *src;
}

char* mycstring::mystrchr (char *str, int c){
        while ((*src++!=c)&&*src);
        src--;
        if(*src==0) return 0;
        return *src;
}
Before the while loop, then?
 
that looks good except we need that stupid if also
no right after the src--
 
No, wait.
I'm confused...
Actually, we can put that inside the other if
 
char* mycstring::mystrchr (char *str, int c){
while ((*src++!=c)&&*src);
src--;
if (c == *str == 0) return str;
if(*src==0) return 0;
return src;
}
oops
 
4:58 AM
if(*src==0) return c==0 ? str : 0;
Ternaries!
 
you guys are missing the HUGE picture that strchr returns an index, not a value
 
@johnathon WHA!?!?!?
AND I QUOTE
A pointer to the first occurrence of character in str.
 
nevemrind
 
that is not an index sir it is a pointer
 
it's late
std::find
same purpose
 
5:00 AM
rofl
 
different reutnrs
 
No, no index.
See the man:
 
char* mycstring::mystrchr (char *str, int c){
while ((*src++!=c)&&*src);
src--;
if (c == *str == 0) return str;
if(*src==0) return 0;
return str;
}
char* mycstring::mystrchr (char *str, int c){
while ((*src++!=c)&&*src);
src--;
if(*src==0) return (c == 0 ? src : 0);
return src;
}
is ternary version
 
 
your prof will be like "what that's too much pr0n for me"
"you must have hired a professional pr0n star to write this"
 
5:02 AM
@Moshe can we move along, i said it's late, and i had std::find on my mind
 
Yea, well, I thought of the ternary myself.
:P
Oh, iTunes, why must you play required listening from Core Music at 1am?
@johnathon Sure, np.
 
why are you guys looping to the end of src?
 
Sec...
Ok, last one is the compare
 
while(*src != chr && src); {} return src;
err
 
Heh
 
5:04 AM
put a src++ in there
in the brackets
 
no
!
that FAILS
 
no
 
if c is 0 it won't return the index
 
it'll return nullptr
which it's supposted to
 
no, if you're searching for 0, it should point to where the 0 is
 
5:05 AM
your not searching for 0
your shearching for the ch
 
you are if c = 0
 
then that will still work because it'll hit the end of the string and give you that pointer
A pointer to the first occurrence of character in str.
If the value is not found, the function returns a null pointer.
by definition, that while(*str != ch && str) {str++;}return str; will work
in fact
if you wanted the acutal index
you'd just subract the return of that, from the origional pointer postion passed in
and you could get the len too
 
how does src become null???
also while(*str!=ch&&*str) you need
 
its not *src, its src
 
so as long as the pointer is valid
 
5:09 AM
dur
anways
 
it'll just chug along for 4 billion places until it wraps around to 0
segfaulting
 
it is *src
 
Haha
 
while(*src != ch && *src) // this tests for the ch, and that the src hasnt hit the end of the string
{ src++; //incriment }
 
5:10 AM
yes we already came up with that
 
return src;
that fits the algorithim
the requirements
 
actually though,
*str++!=c && *src
when will str be incremented in that expression?
by the C standard, if str != c then the right-hand operator will be evaluated
we COULD make it work right but your version is more readable
 
i want it to, to make sure it's not at the end of string
 
while (*src != ch && *src) { src++; } if (*src == 0) return (c == 0 ? src : 0); return src;
 
no, it doesnt need that
because if you incirment to the end of the string
it's at 0
 
5:13 AM
I don't know what you're talking about
doesnt need what?
:-D
 
the if(*src ==0)reutrn (c == 0 ?src : 0)
see the loop
will run until one of two things happen
we find the ch, or we hit the end of the string
 
mmhmmm.
or we find both at the same time
 
A pointer to the first occurrence of character in str.
If the value is not found, the function returns a null pointer.
 
show your completed function!
 
lemme do this in vc
 
5:17 AM
time for a throwdown
but it's too late I'm getting up early tomorrow to look for common yellowthroats
in like 6 hrs and I'm sick today
 
Heh, ok.
 
maybe 5 hrs
 
Thanks for the help folks.
 
I'm too sick to remember
 
Glad you worked out your differences.
 
5:17 AM
@moshe do you think you got it enough?
 
char * strchr(char* src,const char ch)
{
while(*src != ch && *src)
{
src++;
}
return *src==0?0:src;
}
 
that will technically work
you and I just disagree on the corner case
 
@stdOrgnlDave Yea, thanks.
 
:-?? i'm half paying attention anyways
 
go with @johnathon's implementation as long as you understand it. it's better
godspeed with the rest of stuff
2
 
5:26 AM
Thanks.
Good night folks.
 
@Moshe goodnight, gl
 
ty
 
02:00 - 04:0004:00 - 06:00

« first day (38 days earlier)      last day (147 days later) »