In the above code fragment, what would be the cleanest way to add len characters from s2 to s3?Code:char s1[] = "Lorem Ipsum"; char *s2 = s1; while (*s2 != 'e') ++s2; size_t len = 5; char *s3;
In the above code fragment, what would be the cleanest way to add len characters from s2 to s3?Code:char s1[] = "Lorem Ipsum"; char *s2 = s1; while (*s2 != 'e') ++s2; size_t len = 5; char *s3;
First, s3 doesn't point to writeable memory. Second, better example?
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
First, you'd need to make s3 an array so it can hold any characters that you assign to it.
You could use strncat with an =0 at the end. (strncpy if there are no other characters in the target string.)
I think you want to pointers way, however:Code:strncat(s3, s2, len); s3[len] = 0;
That code modifies s3 and s2, of course. (You'd need to put the array elsewhere since you can't increment a pointer.)Code:while(len--) *s3++ = *s2++; *s3 = 0;
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.
strncpy &| strncat with actual writable memory.
Quzah.
Hope is the first step on the road to disappointment.
How aboutOriginally Posted by Dave_SinkulaIn this case, what would be the cleanest way to have s3 == "is" (preferably, including the '\0')?Code:char s1[] = "This is an example sentence."; char *start = s1; while (*start != ' ') ++start; char *end = start; while (*end != ' ') ++end; size_t len = end - start; char *s3 = malloc(len + 1);
See my example and Quzah's cryptic reply; strncpy().
Or you could use sprintf().
Code:sprintf(s3, "%.*s", len, s2);
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.
Code:strncpy(s3, s2, len);Code:sprintf(s3, "%.*s", len, s2);Which is most preferable?Code:while(len--) *s3++ = *s2++; *s3 = 0;
Whichever you choose of the last two -- there's a bug in the first.Originally Posted by Queue
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Well, strncpy() [edit] (+ the =0 as Dave has pointed out) [/edit] would probably be faster than sprintf() because that's its job; printf() would have to parse "%.*s" to figure out what to do. And the second may be slightly faster (or it may not, depending on optimisations); however, it changes s3, which is probably a bad idea, since that's your pointer to the malloc()'d memory.
As an alternative, try this or a variation:
Code:s3[len] = 0; while(len--) s3[len] = s2[len];
Last edited by dwks; 09-16-2006 at 08:28 PM.
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.
Or even memcpy plus null termination.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
There are hundreds of ways to do it, take your pick. They pretty much boil down to a library function (such as strncpy, sprintf, memcpy, memmove, etc) or a loop of some kind.
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.
So it's basically a style/personal choice? Thanks everyone for all the input.Originally Posted by dwks
Yes, pretty much. I usually use a function, possibly one that I wrote myself, because it's easier to read.
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.
I like minimalistic standard functions, because they may be implemented in hand-optimized assembly to do things better than I know, or they might do the same as a simple function I might write -- generally as-good or better.Originally Posted by Queue
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
I guess memcpy() would be the fastest (in theory) because it doesn't need to look for a NULL as strncpy does, it just has to copy the right number of bytes. strncpy() might be better if the string to be copied might contain NULLs.
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.