What is the
here function supposed to do? (Looks like it's supposed to count characters, but the random
++y;
on line 18 is throwing me off)
And are you sure the first code is what's given to you?
There are some errors with it:
14 15 16 17 18 19 20 21
|
int here(const char *s); // Extra semicolon
{
int y;
for(y = o; *s != '\0'; ++s); // o instead of 0, *s instead of *(s+y),
// ++s instead of ++y, extra semicolon at the end
++y;
return y;
}
|
If you want to translate that literally into array notation, it would be
14 15 16 17 18 19 20 21
|
int here(const char s[]); // Extra semicolon
{
int y;
for(y = o; s[0] != '\0'; ++s); // o instead of 0, s[0] instead of s[y],
// ++s instead of ++y, extra semicolon at the end
++y;
return y;
}
|
Either way, neither of the two will actually compile. (But I've put in comments on what's wrong with the code)
If you're trying to count characters, the way to do it is
1 2 3 4
|
int y;
for (y = 0; s[y] != '\0'; ++y) // The ++y here takes care of the counting
{} // Nothing to do here
return y;
|
In contrast, the following:
1 2 3 4
|
int y;
for (y = 0; s[y] != '\0'; ++y)
++y;
return y;
|
will skip over every other character and increment
y twice for each character that it doesn't skip over.
If
s has an odd number of characters, then it'll skip over the terminating null character and venture off into no man's land and (possibly) do crazy stuff.