Search code examples
c++cubuntubluetoothtemporary

C/C++ Warning: address of temporary with BDADDR_ANY Bluetooth library


I'm having some problems with g++ and the compiling process for a C/C++ program which use Bluetooth libraries under Ubuntu.

If i use gcc, it works fine with no warning; on the contrary, if i use g++ i get this warning:

warning: taking address of temporary

even if the program compiles fine and it works.

The involved lines reporting the error are:

        bdaddr_t *inquiry(){
       // do some stuff.. 
    bacpy(&result[mote++], BDADDR_ANY);
    return result;
}
//...
void zeemote(){
while (bacmp(bdaddr, BDADDR_ANY)){
/..
}
}

In both the cases, BDADDR_ANY is involved.

How can i solve this warning?

BDADDR_ANY is defined in bluetooth.h like:

/* BD Address */
typedef struct {
    uint8_t b[6];
} __attribute__((packed)) bdaddr_t;

#define BDADDR_ANY   (&(bdaddr_t) {{0, 0, 0, 0, 0, 0}})

Solution

  • (&(bdaddr_t) {{0, 0, 0, 0, 0, 0}})
    

    Constructs a temporary object and uses its address. This isn't allowed in C++.

    You can fix this by creating a named temporary variable and using bacpy and bacmp on it:

    bdaddr_t tmp = { };
    
    bacpy(&result[mote++], &tmp);
    

    and

    while (bacmp(bdaddr, &tmp)) {
        //
    }