Help needed on NXC

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
floppi
Posts: 48
Joined: 25 Jul 2012, 22:55

Help needed on NXC

Post by floppi »

hi, here something I wrote, I've got a file error when running.
It seems there is a problem accessing the array !

string str[];
int NbStr=0;

void sub();
{
NbStr=0;

if (something)
{
NbStr+=1;
str[NbStr]="Q";
}

if (something)
{
NbStr+=1;
str[NbStr]="R";
}
}

task main()
{
int i;
while (true)
{
sub();
if (NbStr>0)
{
for (i=1;i<=NbStr;i++)
{
TextOut(i*8,LCD_LINE3,str); // here is the line that causes the error !
}
}
}
}

what is wrong there ???
thanks in advance.
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Help needed on NXC

Post by afanofosc »

I think if you allocate your string array to have more than zero elements then you will not get the File Error message.

Code: Select all

string str[10];
But I am not sure what you intend, exactly. Are you trying to grow the string array over time or should it have a fixed size?

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
floppi
Posts: 48
Joined: 25 Jul 2012, 22:55

Re: Help needed on NXC

Post by floppi »

afanofosc wrote:I think if you allocate your string array to have more than zero elements then you will not get the File Error message.

Code: Select all

string str[10];
But I am not sure what you intend, exactly. Are you trying to grow the string array over time or should it have a fixed size?

John Hansen
thanks ! It works !

Fixed size is enough for my use.
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Help needed on NXC

Post by afanofosc »

Something to keep in mind is that with C and many other programming languages the first element in array has an index of zero rather than one. So if NbStr is == 0 then you would modify the first element in the array but if NbStr is == 1 then you would actually modify the second element in the array so its size would need to be at least 2. You should iterate through an array in C from 0 through N-1 where N is the size of the array.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
Post Reply

Who is online

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