Gamestudio Links
Zorro Links
Newest Posts
folder management functions
by VoroneTZ. 04/17/24 06:52
lookback setting performance issue
by 7th_zorro. 04/16/24 03:08
zorro 64bit command line support
by 7th_zorro. 04/15/24 09:36
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:48
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:46
LPDIRECT3DCUBETEXTUR
E9

by Ayumi. 04/12/24 11:00
Sam Foster Sound | Experienced Game Composer for Hire
by titanicpiano14. 04/11/24 14:56
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (7th_zorro), 429 guests, and 0 spiders.
Key: Admin, Global Mod, Mod
Newest Members
11honza11, ccorrea, sakolin, rajesh7827, juergen_wue
19045 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
String help #440283
04/22/14 16:16
04/22/14 16:16
Joined: Jun 2013
Posts: 1,609
D
DdlV Offline OP
Serious User
DdlV  Offline OP
Serious User
D

Joined: Jun 2013
Posts: 1,609
Hi all. There must be something obviously wrong with this snipit, but I can't see it. frown Why is the value of s1 being reset after the last assignment to its value from the prior bar? Thanks.
Code:
function run() {
	set(LOGFILE);
	if (Bar>5) quit();
	string s1	= "12345678901234567890";
	printf("\nBar,s1=%i,%s",Bar,s1);
	s1	= "00";
	printf("\nBar,s1=%i,%s",Bar,s1);
	s1	= "xx";
	printf("\nBar,s1=%i,%s",Bar,s1);
	s1	= "**";
	printf("\nBar,s1=%i,%s",Bar,s1);
	sprintf(s1,"%02d",3*Bar+7);
	printf("\nBar,s1=%i,%s",Bar,s1);
}


Re: String help [Re: DdlV] #440289
04/22/14 18:59
04/22/14 18:59
Joined: Apr 2008
Posts: 585
Austria
Petra Offline
Support
Petra  Offline
Support

Joined: Apr 2008
Posts: 585
Austria
You're overwriting a string constant in the sprintf line.

Re: String help [Re: Petra] #440293
04/22/14 20:03
04/22/14 20:03
Joined: Jun 2013
Posts: 1,609
D
DdlV Offline OP
Serious User
DdlV  Offline OP
Serious User
D

Joined: Jun 2013
Posts: 1,609
Thanks Petra! But I still don't quite follow. Isn't s1 a variable that I can reassign a new value to? What makes it a constant that I shouldn't assign?

What bothers me isn't the sprintf (which seems to work fine), but the assignment of "**" above. What is printed by the following printf isn't "**" (except the 1st time) - what's printed is the value of s1 from the preceding bar.

Thanks.

Re: String help [Re: DdlV] #440311
04/23/14 12:35
04/23/14 12:35
Joined: Jul 2000
Posts: 27,978
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,978
Frankfurt
Your "**" is a string constant. Writing into is is normally not recommended and can even cause a crash. Handling strings is a little more complex than numeric variables, so I suggest a C online course as listed in the manual for learning how to use strings.

In your case a simple solution to avoid a crash would be setting s1 to a long empty string before writing into it, like this:

s1 = "____________________";
sprintf(s1,..);

Re: String help [Re: jcl] #440316
04/23/14 13:47
04/23/14 13:47
Joined: Jun 2013
Posts: 1,609
D
DdlV Offline OP
Serious User
DdlV  Offline OP
Serious User
D

Joined: Jun 2013
Posts: 1,609
Thanks jcl. I understand that "**" is a string constant and s1 is being set to the address of it. (Although anytime one has available the address of something, it's a bit difficult to really call it a "constant". laugh ) But "____________________" is also a string constant so I don't quite see the difference since I didn't sprintf more than 2 bytes in... In any case, I have added your s1 initialization into the code with an additional printf, but the problem persists. After the 1st iteration where the printf displays your initialization string as above, on every subsequent bar your initialization string is not present. In every subsequent bar, s1 reverts to its ending value on the prior bar. Why?

Thanks.

Code:
function run() {
	set(LOGFILE);
	if (Bar>5) quit();
	string s1	= "12345678901234567890";
	printf("\nBar,s1=%i,%s",Bar,s1);
	s1	= "00";
	printf("\nBar,s1=%i,%s",Bar,s1);
	s1	= "xx";
	printf("\nBar,s1=%i,%s",Bar,s1);
	s1	= "**";
	printf("\nBar,s1=%i,%s",Bar,s1);
	s1 = "____________________";
	printf("\nBar,s1=%i,%s",Bar,s1);
	sprintf(s1,"%02d",3*Bar+7);
	printf("\nBar,s1=%i,%s",Bar,s1);
}


Re: String help [Re: DdlV] #440319
04/23/14 14:39
04/23/14 14:39
Joined: Jul 2000
Posts: 27,978
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,978
Frankfurt
Because you're overwriting the constant with that value.

For working with strings, I definitely suggest to read through that part in a C online course - such a course does a much better job in explaining strings than I could do.

Re: String help [Re: jcl] #440323
04/23/14 16:10
04/23/14 16:10
Joined: Jun 2013
Posts: 1,609
D
DdlV Offline OP
Serious User
DdlV  Offline OP
Serious User
D

Joined: Jun 2013
Posts: 1,609
Thanks jcl (& Petra). The paradigm has finally shifted. laugh It's a lousy paradigm, but it is what it is.

Thanks.


Moderated by  Petra 

Gamestudio download | chip programmers | 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