Quote:Original post by honnyjopper
'\0' and NULL are identical here
No, NULL is only guaranteed to work as a pointer constant. In fact, some compilers even define NULL as ((void*)0), which is incompatible with the above usage.
Quote:Original post by honnyjopper
'\0' and NULL are identical here
Quote:What the? I think someone is mixing up NULL (the invalid pointer) and NUL (the terminator character). The literal you're looking for is '\0'.Well, I've never seen a compiler that's as pedantic as to complain about this... plus Visual Assist lights up the NULL macro in purple so I'll stick with my pretty code thank you.
Quote:Your suggestion of avoiding unnecessary traversal stands (even though an strcpy + strcat approach is still linear, and you still have to do those strlen anyway). If I were to handle it as a clean function, I'd probably write:
This is, I believe, the safest you can get in C. In the end, I'd rather spend time getting std::string to work, than rewriting it myself in C...
Quote:Original post by Hidden Asbestos
Well, I've never seen a compiler that's as pedantic as to complain about this... plus Visual Assist lights up the NULL macro in purple so I'll stick with my pretty code thank you.
I also think it reads better, people say 'null terminated string' right? *dest = 0 seems to imply some kind of intent here, like you want to set the character to zero not just wrap up the end of the string.
Quote:But this is for a PIC, I can't imagine that you'd care so much about safety that you'd sacrifice all that time doing strlen and malloc ... maybe some kind of stack alloc function would be a compromise?
Quote:Original post by Anonymous Poster
You can only program pics in c.
Well with the offical Complier anyway.
Quote:Original post by ToohrVykQuote:Original post by Inmate2993...strncat( name, first, sizeof(name)-1 );strncat( name, last, sizeof(name)-1 );
int len = strlen(first) + strlen(last);char *name = malloc(len + 1);strncpy(name, first, len);strncat(name, last, len);name[len] = '\0';
Quote:Original post by Excors
VC2005's strcat_s does do the sane thing (taking the size of the destination buffer as an argument), so it seems quite sensible to write your own version of that.
Quote:Original post by ToohrVyk
it seems that strncat doesn't null-terminate the output if it overflows.
Quote:Serves me right for not using C string manipulation often enough.I think "never" is the optimum frequency at which to use C string manipulation [smile]