Question

uint32 measurements [32];

Xcp_Addr_t XcpApp_ConvertAddress( uint32 address, uint8 extension )
{

    return &measurements[address];  //return from incompatible pointer type 

}

address is the value which i am recieving from the client (example : 0,1,2.....). The above function as to return the address of the measurement to the other internal function. I am getting a warning as below :

return from incompatible pointer type

could anyone tell me how to solve this ?

Was it helpful?

Solution

It all depends on the type of Xcp_Addr_t. Your expression is: &measurements[address]. The type of the expression is convertible to uint32*. If your return type is an uint32 in disguise, then just remove the & operator. If your return type is completely different, you are to rethink what you are doing.


So your typedef is:

typedef uint8* Xcp_Addr_t;

as you can see uint8* (of the return type) and uint32* (the actually returned value's type) don't match. You can either change the return type or the type of the array measurements to:

uint8 measurements[32];

Ok, so you want to ensure that XcpApp_ConvertAddress returns a valid pointer (without going out of bounds). You have two choices:

  • assert it
  • throw an exception

You can assert it by doing:

assert(address < 32);
return &measurements[address];

in this case the program will fail at runtime if the address passed to the function is incorrect (notice that you have to add #include <cassert> to use assert).

Alternatively you can throw an exception:

if (address < 32) throw std::runtime_error("out of bounds");
return &measurements[address];

(notice that you'll need #include <stdexcept> for std::runtime_error).

OTHER TIPS

you have to decide if you want to return the value of measurements[address] or its pointer !

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top