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:
Post
by muntoo » 26 May 2011, 23:22
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]
.
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:
Post
by afanofosc » 27 May 2011, 19:04
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
muntoo
Posts: 834 Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:
Post
by muntoo » 27 May 2011, 22:24
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
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:
Post
by afanofosc » 28 May 2011, 02:23
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
ricardocrl
Posts: 117 Joined: 27 Dec 2010, 19:27
Post
by ricardocrl » 28 May 2011, 12:18
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:
Post
by bullestock » 28 May 2011, 17:20
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
Post
by HaWe » 28 May 2011, 19:39
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
Post
by ricardocrl » 29 May 2011, 21:12
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:
Post
by muntoo » 29 May 2011, 21:29
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++;
.
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
Post
by HaWe » 30 May 2011, 07:11
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)
Users browsing this forum: No registered users and 1 guest