Global variables

13 Sep 2010 . Edited: 13 Sep 2010

Hi, I´m using a global variable i  that can not be changed in the main program

Here is the code:

 

include "mbed.h"

Serial device(p9, p10);  // tx, rx

DigitalOut led3(LED3);

unsigned char buf[1000];
unsigned int i=1;

void rec() {                                             
    buf[i]=device.getc();                                
    i++ ;
}


int main() 
{

device.attach(&rec);                                 
device.baud(19200);
        
    while(1)
        {
            device.printf("AT\r");
            if(buf[i-1]=='0'&&buf[i]==0xD)
            {led3=1;
            i==1;                      
        } 
                     
            wait(6);
        }
        
}

 

When I compile it says that Expression as no effect on main function( i==1; )

Can anyone help me so that I can use the variable i  in the main function

Tanks

SOLVED!

i=0   not i==0   my mistake

Remove this topic please

Tanks

13 Sep 2010

Hi Vitor, All,

Good to hear you solved your problem.

However, I'd encourage you to just reply to your own thread with the solution, rather than deleting the original question. Others are likely to have the same problem so it would help them out in the future! Plus, removing the question means there is no context for the answer!

I've just pasted back in your original question above your answer, so hopefully your efforts will be helping out future mbed users :)

Simon

14 Sep 2010

The wonderful way C / C++ assigns vs compares equality ;=)) How often has that one not causes all of us grief early on and sometimes it still happens....

14 Sep 2010

I agree.. these are very hard to spot...(Especially when you think you're competent enough to not even bother cheking for such errors :P )

14 Sep 2010

At work we try to ensure we have warnings high enough to catch things like this :)

In that case, you'd probably have got a "comparison has no side effect" warning.

Having written C/C++ for 20 years I do it occasionally, mostly due to a genuine mis-key than anything else but it still happens :)