One-Liner
-
A simple example of how not to copy a string... sprintf(packet.data.header.filter, "%s", "wild"); it works, I suppose. Rob Grainger
-
A simple example of how not to copy a string... sprintf(packet.data.header.filter, "%s", "wild"); it works, I suppose. Rob Grainger
The ones I love are these (assuming 8-bit characters) : strncpy( szDestination, szSource, sizeof(szSource) ); or strncpy( szDestination, szSource, strlen(szSource) ); or strncpy( szDestination, szSource, strlen(szDestination) ); X|
-
The ones I love are these (assuming 8-bit characters) : strncpy( szDestination, szSource, sizeof(szSource) ); or strncpy( szDestination, szSource, strlen(szSource) ); or strncpy( szDestination, szSource, strlen(szDestination) ); X|
For us noobs, what is wrong with the last one? At least it uses the destination length.
-
For us noobs, what is wrong with the last one? At least it uses the destination length.
I am noob too, but I dare to guess that it loses terminating zero.
[My Blog]
"Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn
"Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe -
I am noob too, but I dare to guess that it loses terminating zero.
[My Blog]
"Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn
"Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne MetcalfeAh, yes.
-
For us noobs, what is wrong with the last one? At least it uses the destination length.
It works if the current contents of the destination are smaller than the source string. It won't copy entirely if the current destination contents are less than the source, even if the buffer is physically large enough to hold the source. By using strlen, it does not use the full available length of the destination buffer. strlen versus sizeof Judy
-
It works if the current contents of the destination are smaller than the source string. It won't copy entirely if the current destination contents are less than the source, even if the buffer is physically large enough to hold the source. By using strlen, it does not use the full available length of the destination buffer. strlen versus sizeof Judy
Spot on :-D
-
The ones I love are these (assuming 8-bit characters) : strncpy( szDestination, szSource, sizeof(szSource) ); or strncpy( szDestination, szSource, strlen(szSource) ); or strncpy( szDestination, szSource, strlen(szDestination) ); X|
back in K&R C days this was always special: memcpy( dest, src );
-
For us noobs, what is wrong with the last one? At least it uses the destination length.
well, imagine that:
char szDestination[] = { 0xFF, 0xFF }; strncpy(szDestination, szSource, strlen(szDestination));
what do you think would be strlen() here? -- modified at 22:38 Thursday 1st November, 2007 -
well, imagine that:
char szDestination[] = { 0xFF, 0xFF }; strncpy(szDestination, szSource, strlen(szDestination));
what do you think would be strlen() here? -- modified at 22:38 Thursday 1st November, 2007It's an improved random generator!
-
It's an improved random generator!
Steve Hansen wrote:
It's an improved random generator!
ROFL I ought to patent this! Except if you do strncpy() as given you would most likely overwrite the return address or whatever comes next in enclosing function.
-
A simple example of how not to copy a string... sprintf(packet.data.header.filter, "%s", "wild"); it works, I suppose. Rob Grainger
Seen the same dumb stuff to "clear a string":
sprintf( pcSomeStringBuffer, "%s", "");
// Or Even
strcpy( pcSomeStringBuffer, "" );Peace!
-=- James
Please rate this message - let me know if I helped or not! * * *
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
See DeleteFXPFiles