Page 1 of 2

[NXC][Bug] Decrementing char in string

Posted: 26 May 2011, 23:22
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].

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

Posted: 27 May 2011, 19:04
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

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

Posted: 27 May 2011, 22:24
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

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

Posted: 28 May 2011, 02:23
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

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

Posted: 28 May 2011, 12:18
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?

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

Posted: 28 May 2011, 17:20
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.

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

Posted: 28 May 2011, 19:39
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... ;)

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

Posted: 29 May 2011, 21:12
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?

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

Posted: 29 May 2011, 21:29
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++;.

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

Posted: 30 May 2011, 07:11
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)