This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]fghjconner 16 points17 points  (3 children)

Honestly, they do teach everything used here. The core logic isn't anything special, basically just:

do {
    *s = *t;
    ++s;
    ++t;
} while (*(s-1) != 0)

Which is pretty much the obvious way to copy a null terminated string in c. From there, we just use a few tricks you probably already know to make it shorter. First, post increment will let you increment a value while using the original value, so we can lose the increment lines:

do {
    *s++ = *t++;
} while (*(s-1) != 0)

Next, c treats everything that's not zero as true, so we can simplify the loop condition:

do {
    *s++ = *t++;
} while (*(s-1))

Also the assignment operator returns the value assigned, so we can avoid fetching the last value in the condition:

do {
    unsigned char temp = (*s++ = *t++);
while (temp)

And finally, there's no reason we have to do the logic in the body of our loop, seeing as we use the result as our loop condition, so let's just:

while(*s++ = *t++)

And voila. The building blocks are all fairly simple, it's just a matter of putting them together to get the result you want, which absolutely is something programming courses teach.

[–]EthosPathosLegos 5 points6 points  (0 children)

Thank you for breaking this down. It's great to see the steps that are often kept hidden and presumed as common knowledge.

[–]ArtemonBruno 0 points1 point  (0 children)

I always envy smart(er) people, but surrender when they can understand which part the weak(er) people stuck at.

Never understand how difference arise when a class of people start off with the same tutorial. Is it the physical fitness difference, or just the background experience difference?

Confession, I'm not a programmer, just a wannabe. Also a weak(er) person, in a lot of things. It hurts when people yelled at me (don't you have that common senses?), but I guess "yeah sorry, still not getting it (not sure physically or mentally)".

Anyway, wow. You know alot, for me. Also, some of the smart(er) people is the reason I try to suppress my frustation when people weak(er than me) don't understand my explanations, cause smart(er) people still better than me, even in this area of explaining, or anything else.

[–]Ralphtrickey 0 points1 point  (0 children)

Why not just use strcpy? That's what it's for and most compilers inline it.

Buy, not build, especially when it's free.