View unanswered posts | View active topics It is currently Wed Oct 01, 2014 10:24 pm






Reply to topic  [ 6 posts ] 
Array initialization 
Author Message
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Array initialization
I thought I remember somebody reporting something similar but I can't find it. So here it goes.
Code:
int IntArray[3] =
{
    1,
    2,
    3
};

typedef struct
{
    int a;
    int b;
} ELEMENT;

ELEMENT ElemArray[3] =
{
    {1, 2},
    {3, 4},
    {5, 6}
};

task main()
{
    nxtDisplayTextLine(1, "[%d] %d", 0, IntArray[0]);
    nxtDisplayTextLine(2, "[%d] %d", 1, IntArray[1]);
    nxtDisplayTextLine(3, "[%d] %d", 2, IntArray[2]);
   
    nxtDisplayTextLine(5, "[%d] %d, %d", 0, ElemArray[0].a, ElemArray[0].b);
    nxtDisplayTextLine(6, "[%d] %d, %d", 1, ElemArray[1].a, ElemArray[1].b);
    nxtDisplayTextLine(7, "[%d] %d, %d", 2, ElemArray[2].a, ElemArray[2].b);
   
    while (true)
    {
        wait1Msec(1000);
    }
}

Basically, RobotC does not initialize array of structures correctly. The int array was initialized correctly but the ElemArray has all zeros.


Thu Jul 18, 2013 8:34 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Array initialization
Also, there is a related issue too. It seems RobotC cannot initialize an array of char * but an array of string is fine.
For example:
Code:
char *Names[3] =
{
    "abc",
    "def",
    "ghi"
};

string StrNames[3] =
{
    "abc",
    "def",
    "ghi"
};

It gave a compiler error on the char * initialization. This works perfectly fine with other C languages.


Sat Jul 20, 2013 4:43 am
Profile
Site Admin
Site Admin

Joined: Wed Jan 24, 2007 10:42 am
Posts: 610
Post Re: Array initialization
We're currently knee deep in 4.0/IQ/EV3 development at the moment, but we're adding these to our internal bugs list.

If you find anything else, let us know! Thanks as always!

_________________
Timothy Friez
ROBOTC Developer - SW Engineer
tfriez@robotc.net


Mon Jul 22, 2013 3:54 pm
Profile
Rookie

Joined: Tue Dec 11, 2012 9:19 pm
Posts: 38
Post Re: Array initialization
For what it's worth I just discovered this independently.

Came here to see if there was some sort of funky syntax used
for two dimensional arrays of structs in RobotC and found confirmation
that what I was seeing (all struct members zeroed out).

Any update on when 4.0 is coming out for the NXT? I'm also
waiting on the ability to #include header files containing pragmas.

Thanks.


Tue Jan 21, 2014 1:10 am
Profile
Rookie

Joined: Tue Dec 11, 2012 9:19 pm
Posts: 38
Post Re: Array initialization
skatefriday wrote:
For what it's worth I just discovered this independently.

Came here to see if there was some sort of funky syntax used
for two dimensional arrays of structs in RobotC and found confirmation
that what I was seeing (all struct members zeroed out).

Any update on when 4.0 is coming out for the NXT? I'm also
waiting on the ability to #include header files containing pragmas.

Thanks.


This...
Code:
typedef struct example_bug_ {
    int foo;
    int bar;
} example_bug_t;

example_bug_t foobar[] = {
    { 1, 2 },
    { 2, 3 }
};

produces
Code:
*Error**:Wrong number of initialization elements for second array bound of 'foobar'

4.26 update, this appears to still be a bug in 4.x.

Any idea if it might be fixed in the near future?


Sat Sep 27, 2014 11:04 pm
Profile
Rookie

Joined: Tue Dec 11, 2012 9:19 pm
Posts: 38
Post Re: Array initialization
skatefriday wrote:

This...
Code:
typedef struct example_bug_ {
    int foo;
    int bar;
} example_bug_t;

example_bug_t foobar[] = {
    { 1, 2 },
    { 2, 3 }
};

produces
Code:
*Error**:Wrong number of initialization elements for second array bound of 'foobar'

4.26 update, this appears to still be a bug in 4.x.

Any idea if it might be fixed in the near future?


Or rather the original bug appears to have morphed into one
in which the above doesn't compile. I did leave out the array
size, but the compiler should be able to determine that from the
static initializer, just like it can determine a char array size from
a literal string initializer.

This is a really useful construct. Any clue whether it's on the
feature list?


Sun Sep 28, 2014 12:30 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 6 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.