String help

Posted By: DdlV

String help - 04/22/14 16:16

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);
}

Posted By: Petra

Re: String help - 04/22/14 18:59

You're overwriting a string constant in the sprintf line.
Posted By: DdlV

Re: String help - 04/22/14 20:03

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.
Posted By: jcl

Re: String help - 04/23/14 12:35

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,..);
Posted By: DdlV

Re: String help - 04/23/14 13:47

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);
}

Posted By: jcl

Re: String help - 04/23/14 14:39

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.
Posted By: DdlV

Re: String help - 04/23/14 16:10

Thanks jcl (& Petra). The paradigm has finally shifted. laugh It's a lousy paradigm, but it is what it is.

Thanks.
© 2024 lite-C Forums