| Author |
Message |
[Gandalf]

|
Posted: Tue Dec 12, 2006 11:02 pm Post subject: (No subject) |
|
|
Nope, no edit button in help forums because it was abused.
wtd wrote: Much simpler than that, Gandalf.
Do you understand how the strcpy function works?
Well, I assumed so. Even simpler than Andy's too? Something with recursion maybe?
And yes, I'm quite confident in my understanding of your strcpy function. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
md

|
Posted: Wed Dec 13, 2006 12:51 am Post subject: (No subject) |
|
|
| c++: |
char *cat(char *dest, char *src)
{
while( *dest ) dest++;
while( *dest++ = *src++ ) ;
return dest;
}
|
please excuse any typos... beer makes typing hard. |
|
|
|
|
 |
Andy
|
Posted: Wed Dec 13, 2006 5:44 am Post subject: (No subject) |
|
|
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 |
|
|
|
|
 |
bugzpodder

|
Posted: Wed Dec 13, 2006 9:29 am Post subject: (No subject) |
|
|
Andy wrote: tsk tsk gandalf...
| c++: |
void strcat(char *dest, char *src)
{
while (*dest++ = (*dest)?*dest:*src++);
}
|
tsk tsk Andy...
| c++: |
while ((*d++) || *d = *s++);
|
|
|
|
|
|
 |
Monstrosity_
|
Posted: Wed Dec 13, 2006 11:43 am Post subject: (No subject) |
|
|
Andy wrote: md, you cant return a pointer when your functions shifts it.
And why not? |
|
|
|
|
 |
md

|
Posted: Wed Dec 13, 2006 11:43 am Post subject: (No subject) |
|
|
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--;
I think bugzpodder's code is the shorted however... |
|
|
|
|
 |
wtd
|
Posted: Wed Dec 13, 2006 12:15 pm Post subject: (No subject) |
|
|
md wrote: I think bugzpodder's code is the shorted however...
And yet represents more of a change than the code I demonstrated to you.  |
|
|
|
|
 |
bugzpodder

|
Posted: Wed Dec 13, 2006 12:44 pm Post subject: (No subject) |
|
|
the correct code should actually be:
[syntax]
while(*++d||(*d=*s++));
[/code]
because of a subtlety *++d is evaluated from left to right, so d gets deferenced first then increased
Quote: And yet represents more of a change than the code I demonstrated to you. What do you mean? |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Monstrosity_
|
Posted: Wed Dec 13, 2006 1:00 pm Post subject: (No subject) |
|
|
bugzpodder wrote: the correct code should actually be:
[syntax]
while(*++d||(*d=*s++));
[/code]
because of a subtlety *++d is evaluated from left to right, so d gets deferenced first then increased
The grouping for those two is right to left, (*(++d)). If it were (++(*d)), where d gets dereferenced first then incremented, then your stuck in loop where the pointer never changes.
Also, your increments in both examples would skip assigning/checking to the first value in the array. Try with d as an empty string.
wtd wrote: md wrote: I think bugzpodder's code is the shorted however...
And yet represents more of a change than the code I demonstrated to you. 
Hmm, I'm interested in what yours could be now. I would have just added another line to the strcpy function. |
|
|
|
|
 |
bugzpodder

|
Posted: Wed Dec 13, 2006 1:23 pm Post subject: (No subject) |
|
|
I was too greedy. Tried to put everything in the conditional statement.
| code: |
while(*d||(*d=*s++))d++;
|
Take that, punk!  |
|
|
|
|
 |
bugzpodder

|
Posted: Wed Dec 13, 2006 1:28 pm Post subject: (No subject) |
|
|
| and before anyone claims andy's code is shorter, both code sets has comparable character counts, and my code do |d| less assignments than andy's code |
|
|
|
|
 |
wtd
|
Posted: Wed Dec 13, 2006 1:33 pm Post subject: (No subject) |
|
|
| code: | void strcat(char *dest, char *src)
{
while (*dest) dest++;
while (*dest++ = *src++);
} |
This represents a very minimal change to the strcpy function as provided. |
|
|
|
|
 |
Monstrosity_
|
Posted: Wed Dec 13, 2006 1:40 pm Post subject: (No subject) |
|
|
wtd wrote: | code: | void strcat(char *dest, char *src)
{
while (*dest) dest++;
while (*dest++ = *src++);
} |
This represents a very minimal change to the strcpy function as provided.
I would have done
| code: | void strcat(char *dest, char *src)
{
dest += strlen(dest);
while (*dest++ = *src++);
} |
same deal, just function call over head ;p |
|
|
|
|
 |
bugzpodder

|
Posted: Wed Dec 13, 2006 1:44 pm Post subject: (No subject) |
|
|
| then you might as well use memcpy |
|
|
|
|
 |
wtd
|
Posted: Wed Dec 13, 2006 2:00 pm Post subject: (No subject) |
|
|
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? |
|
|
|
|
 |
|