c++ - incompatible pointer type warning in c? -
uint32 measurements [32]; xcp_addr_t xcpapp_convertaddress( uint32 address, uint8 extension ) { return &measurements[address]; //return incompatible pointer type } address value recieving client (example : 0,1,2.....). above function return address of measurement other internal function. getting warning below :
return incompatible pointer type could tell me how solve ?
it depends on type of xcp_addr_t. expression is: &measurements[address]. type of expression convertible uint32*. if return type uint32 in disguise, remove & operator. if return type different, rethink doing.
so typedef is:
typedef uint8* xcp_addr_t; as can see uint8* (of return type) , uint32* (the returned value's type) don't match. can either change return type or type of array measurements to:
uint8 measurements[32]; ok, want ensure xcpapp_convertaddress returns valid pointer (without going out of bounds). have 2 choices:
- assert it
- throw exception
you can assert doing:
assert(address < 32); return &measurements[address]; in case program fail @ runtime if address passed function incorrect (notice have add #include <cassert> use assert).
alternatively can throw exception:
if (address < 32) throw std::runtime_error("out of bounds"); return &measurements[address]; (notice you'll need #include <stdexcept> std::runtime_error).
Comments
Post a Comment