del62
Deity
I'm learning C at the moment, and it really messes my brain up.
It'd be probably wonderful for first language, and learned at slow pace, but I have Java burden, and the aim is to learn it in six weeks (first week basics, second week pointers, third week input/output...)
Here's one of the many things I don't understand, from Kerninghan & Ritchie's book:
Ok, I understand perfectly well what this is doing, but suppose that s is shorter than t. Doesn't this write to memory locations that aren't allowed? Once we pass s's '\0' that is.
Here's an illustration,
SSUUUTTT
There's two bytes for s, four for t and in between three for U. If we copy t to s, doesn't this write over first Us?
/* Copies string t to s */
void strcopy(char *s, char* t)
{
while ((*s++ = *t++) != '\0')
;
}
Of course c using pointers allows you to overwrite memory outside the bounds of the array, which is why great care needs to be taken, this means you have to do the bounds checking yourself
if t pointed to an area in memory that immediately preceeded where s pointed to in memory and there was no NUL \o then it is concevable that this would keep going in your while loop until some fatal memory exception.