memset with offset possible ?
Author |
Message |
Peter Hans #1 / 7
|
memset with offset possible ?
Is there a way to specify that I want to copy a src string NOT to the beginning of a target string but let the copy begin at say position 5 of the target ? In other words: Is there a fourth parameter offset like memset(buffer, srctext, len, offset) Does this operation start the copy from position offset in srctext or does offset mean the first position in buffer which ist filled with srctext ? Thx Peter
|
Sat, 02 Apr 2005 02:56:12 GMT |
|
|
Joona I Palast #2 / 7
|
memset with offset possible ?
on comp.lang.c: Quote: > Is there a way to specify that I want to copy a src string NOT to the beginning of a target > string but let the copy begin at say position 5 of the target ? > In other words: Is there a fourth parameter offset like > memset(buffer, srctext, len, offset) > Does this operation start the copy from position offset in srctext or does offset > mean the first position in buffer which ist filled with srctext ?
The "buffer" parameter of memset is a pointer, but memset doesn't care where you get that pointer from. So it's quite possible to make it point after the start of your allocated block: memset(buffer + offset, srctext, len-offset); --
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++| | http://www.helsinki.fi/~palaste W++ B OP+ | \----------------------------------------- Finland rules! ------------/ "It's not survival of the fattest, it's survival of the fittest." - Ludvig von Drake
|
Sat, 02 Apr 2005 03:33:28 GMT |
|
|
Jonas Lindstr? #3 / 7
|
memset with offset possible ?
alt.comp.lang.learn.c-c++: Quote:
> on comp.lang.c: >> Is there a way to specify that I want to copy a src string >> NOT to the beginning of a target string but let the copy >> begin at say position 5 of the target ? In other words: Is >> there a fourth parameter offset like >> memset(buffer, srctext, len, offset) >> Does this operation start the copy from position offset in >> srctext or does offset mean the first position in buffer >> which ist filled with srctext ? > The "buffer" parameter of memset is a pointer, but memset > doesn't care where you get that pointer from. So it's quite > possible to make it point after the start of your allocated > block: > memset(buffer + offset, srctext, len-offset);
Four. --
Stockholm, Sweden
|
Sat, 02 Apr 2005 03:35:58 GMT |
|
|
willem veenhove #4 / 7
|
memset with offset possible ?
Quote:
> memset(buffer + offset, srctext, len-offset);
You'd better look up memset in your favorite C book or drink a cup of coffee :) willem
|
Sat, 02 Apr 2005 03:38:00 GMT |
|
|
Joona I Palast #5 / 7
|
memset with offset possible ?
on comp.lang.c: Quote:
>> memset(buffer + offset, srctext, len-offset); > You'd better look up memset in your favorite C book or drink > a cup of coffee :)
Drat! Thanks for noticing. I was thinking memcpy when I saw memset. It was that darned OP, he did it sir, he confused me. =) --
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++| | http://www.helsinki.fi/~palaste W++ B OP+ | \----------------------------------------- Finland rules! ------------/ "A computer program does what you tell it to do, not what you want it to do." - Anon
|
Sat, 02 Apr 2005 03:52:29 GMT |
|
|
Allin Cottrel #6 / 7
|
memset with offset possible ?
Quote:
> Is there a way to specify that I want to copy a src string NOT to the beginning of a target > string but let the copy begin at say position 5 of the target ? > In other words: Is there a fourth parameter offset like > memset(buffer, srctext, len, offset) > Does this operation start the copy from position offset in srctext or does offset > mean the first position in buffer which ist filled with srctext ?
memset fills the target with a constant byte, so it is not the function you want. How about memcpy, str[n]cpy() or str[n]cat()? As for the offset, that's trivial: strncpy(targ + 5, src, len); or memcpy(targ + 5, src, len); Allin Cottrell.
|
Sat, 02 Apr 2005 03:34:26 GMT |
|
|
Dave O'Hea #7 / 7
|
memset with offset possible ?
Quote:
> Is there a way to specify that I want to copy a src string NOT to the > beginning of a target string but let the copy begin at say position 5 > of the target? In other words: Is there a fourth parameter offset like > memset(buffer, srctext, len, offset)
memset is a C function, and C functions can't overload the signature like that. But you don't need it; you can just do this: memset(buffer + offset, srctext, len); This will work if sizeof(buffer[0]) == 1. For example, if it's a char pointer. It won't work on a void pointer, as 'void' has no size, and it might not do what you want if it's a pointer to something else, something larger than a char. I'd read up more about pointer arithmetic before writing lots of code like this. Quote: > Does this operation start the copy from position offset in srctext or > does offset mean the first position in buffer which ist filled with > srctext ?
memset isn't a copy function; it spams the same byte over and over into the destination area. Maybe you meant memcpy and memmove instead? It doesn't matter, though. The "+ offset" for a pointer thing is part of both C and C++, and it is generally useful when you are writing low-enough level code that you want a memset. Also, the syntax for it is used by some STL containers, so it is good to get used to it, at least to be able to recognize it. -- Dave O'Hearn
|
Sat, 02 Apr 2005 09:36:16 GMT |
|
|
|