Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 C TYS
Index -> Programming, C++ -> C++ Help
Goto page Previous  1, 2, 3
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
md




PostPosted: 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
Sponsor
sponsor
md




PostPosted: Wed Dec 13, 2006 2:21 pm   Post subject: (No subject)

[edit type follow up] damned help forums...

code showing a case where your loop fails bugz:
c++:
#include <stdio.h>
#include <string.h>

void cat(char *d, char*s) {
while( *d || ( *d = *s++ ) )
        d++;
}
 

main()
{     
        char dest[40];
        char src[5];

        strcpy(dest, "start   invisible ");
        dest[6] = '\0';

        strcpy(src, "end");

        cat(dest, src);
        printf("%s", dest);
}
bugzpodder




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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. Wink

Anyone care to guess why?


works for me...
Monstrosity_




PostPosted: 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. Wink

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




PostPosted: 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
Sponsor
sponsor
wtd




PostPosted: 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". Smile
Monstrosity_




PostPosted: 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". Smile

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. Smile
Monstrosity_




PostPosted: 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?
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 3 of 3  [ 41 Posts ]
Goto page Previous  1, 2, 3
Jump to:   


Style:  
Search: