[NXC][Bug] Decrementing char in string

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

[NXC][Bug] Decrementing char in string

Post by muntoo »

The following code doesn't compile:

Code: Select all

task main()
{
    string name = "MineCraft";
    --name[3];
}
The following code does:

Code: Select all

task main()
{
    string name = "MineCraft";
    name[3]--;
}
Is this a bug, or do my eyes C wrong?

-----

If there's any ambiguity, please pretend that there are () brackets around name[3].
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: [NXC][Bug] Decrementing char in string

Post by afanofosc »

I would call this a not exactly C limitation. My support for prefix increment and decrement are kind of hacks. You may have to live with this unless a simple solution jumps out at me.

Did you verify that the second case actually generates functioning code or does it just compile but not work?

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: [NXC][Bug] Decrementing char in string

Post by muntoo »

afanofosc wrote:Did you verify that the second case actually generates functioning code or does it just compile but not work?
It looks like it generates the correct NBC code, but I'll test if it actually works tommorow:

Code: Select all

dseg	segment
	__signed_stack_001main	sdword	
	__unsigned_stack_002main	dword	
	____initialize_global_data_return	byte	
	__constVal1	sbyte	1
	__constStr0009	byte[]	'MineCraft'
	__main_7qG2_name_7qG2_000	byte[]	
	__strbufmain	byte[]	
dseg	ends

thread main
	subcall __initialize_global_data, ____initialize_global_data_return
	strcat __strbufmain, __constStr0009
	mov __main_7qG2_name_7qG2_000, __strbufmain
	set __signed_stack_001main, 3
	index __unsigned_stack_002main, __main_7qG2_name_7qG2_000, __signed_stack_001main
	sub __unsigned_stack_002main, __unsigned_stack_002main, __constVal1
	replace __main_7qG2_name_7qG2_000, __main_7qG2_name_7qG2_000, __signed_stack_001main, __unsigned_stack_002main
	exit -1, -1
endt

subroutine __initialize_global_data
	subret ____initialize_global_data_return
ends
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: [NXC][Bug] Decrementing char in string

Post by afanofosc »

That code definitely should work. Thanks for checking. I'll take a peek at how my prefix decrement/increment code works and see if it could be expanded to work with things other than simple variables.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
ricardocrl
Posts: 117
Joined: 27 Dec 2010, 19:27

Re: [NXC][Bug] Decrementing char in string

Post by ricardocrl »

By the way, what should happen with a prefix decrementing/incrementing? I've never seen that in C. Is it from C++ or something other than pure C?
bullestock
Posts: 27
Joined: 29 Sep 2010, 19:34
Location: Denmark
Contact:

Re: [NXC][Bug] Decrementing char in string

Post by bullestock »

ricardocrl wrote:By the way, what should happen with a prefix decrementing/incrementing? I've never seen that in C. Is it from C++ or something other than pure C?
Both postfix and prefix increment/decrement is part of Standard C (and Standard C++).

See http://en.wikipedia.org/wiki/Increment_ ... _operators for details.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: [NXC][Bug] Decrementing char in string

Post by HaWe »

yes, prefix is often used for assignings after increment while postfix assignes before incrementing:
j=i++; // assignes (old) i before incrementing
j=++i; // assignes (new) i+1 (after incrementing)

Maybe a "real" ANSI C compiler that compiles directly to bytecode would make it sometimes easier to use ANSI C syntax... ;)
ricardocrl
Posts: 117
Joined: 27 Dec 2010, 19:27

Re: [NXC][Bug] Decrementing char in string

Post by ricardocrl »

It seems confusing to the eyes, to see that kind of operations (y = ++x;), because it's two at "once". I normally only use "x++;" for instance.

Is there any advantage of writing "y = ++x" instead of using just "x++;" plus "y = x", besides making the code smaller?
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: [NXC][Bug] Decrementing char in string

Post by muntoo »

ricardocrl wrote:Is there any advantage of writing "y = ++x" instead of using just "x++;" plus "y = x", besides making the code smaller?
Not really. It all becomes the same assembly code in the end.

BTW, it's better practice to use ++x; instead of x++;.
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: [NXC][Bug] Decrementing char in string

Post by HaWe »

I'm not sure if the both following do the same (in C, not in C++):

Code: Select all

for(int i = 0; i < 42; i++) {printf("%d\n", i}
// vs.
for(int i = 0; i < 42; ++i) {printf("%d\n", i}
(not at home, can't test it)
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 1 guest