Gamestudio Links
Zorro Links
Newest Posts
Zorro 2.70
by jcl. 09/29/25 09:24
optimize global parameters SOLVED
by dBc. 09/27/25 17:07
ZorroGPT
by TipmyPip. 09/27/25 10:05
assetHistory one candle shift
by jcl. 09/21/25 11:36
Plugins update
by Grant. 09/17/25 16:28
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
2 registered members (AndrewAMD, TipmyPip), 12,420 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
krishna, DrissB, James168, Ed_Love, xtns
19168 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Whats wrong with this? #140369
07/10/07 17:19
07/10/07 17:19
Joined: Jul 2006
Posts: 783
London, UK
sheefo Offline OP
User
sheefo  Offline OP
User

Joined: Jul 2006
Posts: 783
London, UK
I cannot figure out why this code doesn't work. It should replace every 's' character with a 'f' character. It manages to replace once then terminates the string directly after.

Example: "This is a test string!" becomes "Thif", when it should be "Thif if a teft ftring!".

Code:

char* chstrswithf(char* a)
{
while(*a != '\0')
{
if(*a == 's')
*a = 'f';
a++;
}
return a;
}



Re: Whats wrong with this? [Re: sheefo] #140370
07/11/07 10:23
07/11/07 10:23
Joined: Jul 2006
Posts: 783
London, UK
sheefo Offline OP
User
sheefo  Offline OP
User

Joined: Jul 2006
Posts: 783
London, UK
People!?

Re: Whats wrong with this? [Re: sheefo] #140371
07/11/07 17:11
07/11/07 17:11
Joined: Aug 2000
Posts: 1,140
Baunatal, Germany
Tobias Offline

Moderator
Tobias  Offline

Moderator

Joined: Aug 2000
Posts: 1,140
Baunatal, Germany
This code looks ok to me, how do you call it?

Re: Whats wrong with this? [Re: Tobias] #140372
07/11/07 17:55
07/11/07 17:55
Joined: Jul 2006
Posts: 783
London, UK
sheefo Offline OP
User
sheefo  Offline OP
User

Joined: Jul 2006
Posts: 783
London, UK
Code:

char string[32];
strcpy(string, "This is a test string!");
chstrswithf(string);

printf(string); // => "Thif"



Re: Whats wrong with this? [Re: sheefo] #140373
07/12/07 08:39
07/12/07 08:39
Joined: Jan 2007
Posts: 63
Oregon
mbartman Offline
Junior Member
mbartman  Offline
Junior Member

Joined: Jan 2007
Posts: 63
Oregon
Well I know how I would ordinarily do it with strings..

Code:

string chstrswithf(string a)
{
int temp_pos = 0;
while(a[temp_pos] != '\0')
{
if(a[temp_pos] == 's')
a[temp_pos] = 'f';
a++;
}
return a;
}



BUT Lite-C doesn't seem to support indexing a string..

It's late though, so I hope you get this figured out.. Perhaps I will look at it again when I get up.

Good luck,


Michael Bartman A6/A7 Commercial Edition Programmer By Nature
Re: Whats wrong with this? [Re: mbartman] #140374
07/12/07 09:56
07/12/07 09:56
Joined: Jul 2000
Posts: 28,024
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 28,024
Frankfurt
mbartman: Your modification does not make much sense in this context - *a and a[0] are equivalent in C/C++.

Sheefo: Your code is absolutely correct. The reason for the problem is a bug in the lite-C compiler. The line

*a = 'f';

writes an integer into memory, not a char. Therefore the 'f' and three zeros are written, which truncates your string behind the 'f'.

This will be fixed in the next release. Thank you for finding this bug.

Workaround: memset(a,'f',1);

Re: Whats wrong with this? [Re: jcl] #140375
07/12/07 16:50
07/12/07 16:50
Joined: Jan 2007
Posts: 63
Oregon
mbartman Offline
Junior Member
mbartman  Offline
Junior Member

Joined: Jan 2007
Posts: 63
Oregon
You're right jcl, I shouldn't be coding that late at night, it's dangerous lol


Michael Bartman A6/A7 Commercial Edition Programmer By Nature
Re: Whats wrong with this? [Re: mbartman] #140376
07/12/07 18:05
07/12/07 18:05
Joined: Jul 2006
Posts: 783
London, UK
sheefo Offline OP
User
sheefo  Offline OP
User

Joined: Jul 2006
Posts: 783
London, UK
I thought my code was right

The workaround works, so I am happy

Thanks jcl.


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1