Posted: Wed Dec 13, 2006 2:02 pm Post subject: (No subject)
I may have an issue with your code bugz (haven't tested it yet...) What if d has non-zero characters after the first '\0'? seems like your code would consider that part of the string and not copy s over it as should happen.
/me also notes that the code he psoted was given by wtd... after much though and almost having the same thing by me
Sponsor Sponsor
md
Posted: Wed Dec 13, 2006 2:21 pm Post subject: (No subject)
Posted: Wed Dec 13, 2006 3:40 pm Post subject: (No subject)
my code is based on andy's and i believe he put a disclaimer up on this issue, which was known to me. Note the "tsk tsk Andy"
bugzpodder
Posted: Wed Dec 13, 2006 4:14 pm Post subject: (No subject)
and heres a patched version md
code:
while(*d||((*d=*s++) && !(*(d+1)=0)))d++;
Andy
Posted: Wed Dec 13, 2006 4:46 pm Post subject: (No subject)
md wrote:
Andy wrote:
ohh, multiple lines! lol.
md, you cant return a pointer when your functions shifts it. but i'm sure that was just the beer thinking =P
Sure you can! though it should really be return dest--;
err what? at the end of your function, dest is at the end of the string. without knowing how long the string is, you cant set it the pointer back to the beginning
Andy
Posted: Wed Dec 13, 2006 4:49 pm Post subject: (No subject)
wtd wrote:
Monstrosity_ wrote:
code:
void strcat(char *dest, char *src)
{
dest += strlen(dest);
while (*dest++ = *src++);
}
This actually won't work.
Anyone care to guess why?
works for me...
Monstrosity_
Posted: Wed Dec 13, 2006 5:10 pm Post subject: (No subject)
wtd wrote:
Monstrosity_ wrote:
code:
void strcat(char *dest, char *src)
{
dest += strlen(dest);
while (*dest++ = *src++);
}
This actually won't work.
Anyone care to guess why?
strlen is not declared. I figured that would be a given, but in all fairness this could cause trouble if you leave it up to the implicit function declaration.
Andy
Posted: Wed Dec 13, 2006 8:23 pm Post subject: (No subject)
oh! because if you include string.h, the function strcat with the same parameters would have existed already.
Sponsor Sponsor
wtd
Posted: Wed Dec 13, 2006 8:25 pm Post subject: (No subject)
If you include string.h to get the strlen function, then you'd have mismatched types for strcat, as that is also present in string.h, but is not declared to return "void".
Monstrosity_
Posted: Thu Dec 14, 2006 1:15 pm Post subject: (No subject)
wtd wrote:
If you include string.h to get the strlen function, then you'd have mismatched types for strcat, as that is also present in string.h, but is not declared to return "void".
True, _IF_ you include string.h. The code I had pasted didn't, so all it's missing is a declaration for strlen. Which of course the easiest and most portable way is to include the header, but not the only way.
Monstrosity_
Posted: Thu Dec 14, 2006 1:20 pm Post subject: (No subject)
And if you want to be even more pedantic then you can say the code is undefined because it uses a reserved identifier, but then how fun would the problem be?