Just bought the K&R "old testament" C book and was reading along. I came to one of the exercises though and became a bit stumped. It wants you to make an implementation of strcat using pointers. Is this possible to do without using sizeof/malloc ?
Just bought the K&R "old testament" C book and was reading along. I came to one of the exercises though and became a bit stumped. It wants you to make an implementation of strcat using pointers. Is this possible to do without using sizeof/malloc ?
Pass in an array that has more space than it needs.
you can use a very large array and hope it's large enough to hold both strings. Other than that I can't think of a way to do with without sizeof and malloc. Why don't you want to use them?
EDIT: Sorry, I think I need to go drink some coffee :S like the rest said, you just need to append it, not sizeof or malloc needed
Last edited by Abda92; 07-24-2008 at 11:26 AM.
I might not be a pro, but I'm usually right
Why do you need malloc()? strcat() doesn't allocate any memory, it just takes one string and appends it to the end of another.
And besides, strcat is _supposed_ to trash memory if you don't pass it the right thing anyway, right?
What tabstop means is that if your implementation overwrites the end of the array when said array isn't big enough to have the given string stuck onto the end of it, then you're emulating strcat() precisely.
BTW -- you can create a version with memory allocation by using realloc(), though of course you'll always have to pass a valid malloc()'d pointer to the function.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Thanks
That being said does anybody wanna chime in if my code is at all close to being the most efficient for this command?
Code:#include <stdlib.h> void strcats(char * s, char * t); int main() { char one[500] = "Hi there "; char * two = "my name is Jeff"; strcats(one, two); printf("%s", one); } //Copies string t onto end of string s void strcats(char * s, char * t) { for (; *s != '\0'; s++) //Find end of the string in S by pointing to that element { } for (; *t != '\0'; t++, s++) //Loop to the end of T, concatenating char by char to S { *s = *t; } }
strcat() should add a \0 at the end as well.
I seeThanks. Btw anybody that doesn't have this book should get it, way better then regular vanilla C books. Learning 100% more then other books i've read.
Just one thing besides the '\0': the second parameter to strcat() is const, because it never gets modified. Assuming you want an exact replacement for strcat().
[edit] If you want to be really picky . . . printf() is in stdio.h. [/edit]
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Sorry I usually forget to declare things constI guess because it is not required.