ROBOTC.net forums
http://www.robotc.net/forums/

element access for arrays not working with paranthesis
http://www.robotc.net/forums/viewtopic.php?f=1&t=5840
Page 1 of 1

Author:  felix-freiberger [ Sun May 12, 2013 1:35 pm ]
Post subject:  element access for arrays not working with paranthesis

Hi,

in RobotC 3.6, this code compiles:

Code:
task main()
{
   string test[] = {"entry1", "entry2", "entry3", "another one", "final one"};
   nxtDisplayCenteredTextLine(0, "< %s >", test[2]);
}


while this doesn't:

Code:
task main()
{
   string test[] = {"entry1", "entry2", "entry3", "another one", "final one"};
   nxtDisplayCenteredTextLine(0, "< %s >", (test)[2]);
}


Is that a bug? How can I dereference a pointer to an array and look up an element of the array? (*testPointer)[2] won't work.

--Felix

Author:  mightor [ Sun May 12, 2013 4:17 pm ]
Post subject:  Re: element access for arrays not working with paranthesis

What exactly are you trying to achieve? The following code does exactly what it is supposed to do:
Code:
task main()
{
   string test[] = {"entry1", "entry2", "entry3", "another one", "final one"};
   for (int i = 0; i < 5; i++)
   {
     writeDebugStreamLine("< %s >", test[i]);
   }
}

What are you looking to dereference and why? This also works quite nicely:
Code:
task main ()
{
   string test[] = {"entry1", "entry2", "entry3", "another one", "final one"};
   string *foo = &test;
   
   for (int i = 0; i < 5; i++)
   {
     writeDebugStreamLine("< %s >", *(foo + i));
   }
 }

Author:  felix-freiberger [ Mon May 13, 2013 5:25 am ]
Post subject:  Re: element access for arrays not working with paranthesis

Hi,

that seems to work for me, thanks!

I was trying to do something like that:

Code:
nxtDisplayCenteredTextLine(3, "< %s >", *((menu->labels) + (*selection)));


--Felix

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/