Page 1 of 1

Help needed on NXC

Posted: 23 Sep 2012, 17:41
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.

Re: Help needed on NXC

Posted: 23 Sep 2012, 18:01
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

Re: Help needed on NXC

Posted: 23 Sep 2012, 18:03
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.

Re: Help needed on NXC

Posted: 23 Sep 2012, 20:03
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