Reputation: 6555
I'm trying to use sprintf this way:
DebugLogMsg10 (DebugStruct, sizeThreadID, "szLoginHeadString before:\r\n%s",szLoginHeadString );
sprintf (szLoginHeadString, "<br/>%s", szLoginHeadString);
DebugLogMsg10 (DebugStruct, sizeThreadID, "szLoginHeadString after:\r\n%s",szLoginHeadString );
I don't understand the resulting strings.
[Thu Feb 20 14:49:01 2014][Thread:0] szLoginHeadString before:
<form name="login_from" action="http://PLACEHOLDER.com" method="post" accept-charset="ISO-8859-15">
[Thu Feb 20 14:49:01 2014][Thread:0] szLoginHeadString after:
<br/><br/> name="login_from" action="http://http://PLACEHOLDER.com" method="post" accept-charset="ISO-8859-15">
I would be expecting something like
<br/><form name="login_from".....
What am I not thinking about?
Upvotes: 0
Views: 323
Reputation: 499
It might be relative to your C library, but in common sprintf is operating with your memory, not copying string before edit, so at begin it overwrites beginning of your string woth "
" and then adds your string (already overwrited) to itself. So you have two
`s and no .
Do not use one string as source and as output in the same time.
Upvotes: 1
Reputation: 21233
Your code invokes undefined behavior. The sprintf
family functions cannot be given arguments that overlap in memory, as described in the manpages:
C99 and POSIX.1-2001 specify that the results are undefined if a call to sprintf(), snprintf(), vsprintf(), or vsnprintf() would cause copying to take place between objects that overlap (e.g., if the target string array and one of the supplied input arguments refer to the same buffer).
Upvotes: 4