FidoNet Echomail Archive
aust_c_here

<<< Previous Index Next >>>

From: Adam Fitzpatrick
To: Joshua Marshall
Date: 1997-01-03 02:24:24
Subject: sprintf function

Quoting Joshua Marshall to All:

JM>Can somebody tell me the easiest way to do this:

JM>I want to insert a character into a string.  I have tried:

JM>sprintf(string, "%c%c%s", string[0], '0', string+1);

sprintf(fred,"%c%c%s",string[0],'0',string+1);
strcpy(string,fred);

JM>however if string is "TEST" instead of "T0EST" I
am getting "T0000"

This is because the string is being changed. When you pass string+1,
you're passing a pointer to the second character in the array. That
character will have been overwritten by the '0'. Now it's going to
copy the '0' over the 3rd character, and then copy the 3rd character (now
'0') over the 4th, etc.

JM>This is using SAS/C v 6.57 for the Amiga.  Can somebody tell me if
JM>it's just the compiler? 

I'd be surprised if you could find a compiler that would do what you
expect.


... Programmer: the guy who's expected to do what the salesman lied about.
--- FMail/Win32 1.22
 * Origin: Power BBS - +61-2-858-4114 - Files/Games/EchoMail/Internet (3:711/953)
SEEN-BY: 633/267 270
@PATH: 711/953 410 808 712/624 50/99 635/728 633/267



<<< Previous Index Next >>>