Abhinav
Abhinav

Reputation: 109

error: invalid operands to binary -

This piece of code is giving the error error: invalid operands to binary -:

if(memcmp(params , DEV_SERVICE_DISCOVERY , strlen(DEV_SERVICE_DISCOVERY)) == 0)
{
    UINT8 *yes_no;

    params = XSH_UtilFindNextToken(params);
    yes_no = XSH_UtilFindNextToken(params);

    params[yes_no - params - 1] = '\0';

    rc = AppTest_ServiceDiscovery(params, yes_no , strlen(yes_no), pOut);
}

I changed UINT8 *yes_no to char *yes_no and it got resolved. But didn't get why error was coming and why it got resolved. Please explain the issue.

Thanks in advance.

Upvotes: 0

Views: 464

Answers (1)

John Zwinck
John Zwinck

Reputation: 249153

When subtracting two pointers, they must be of the same type. This is because pointer arithmetic in C is based on the size of the pointees, so for example if you subtract char* from int* you have a 1-byte object and a 4-byte object, and so there is no way for the compiler to know whether the result should be in 1-byte units or 4-byte units.

I realize your two pointees are both single bytes, but it's still not allowed in standard C (which makes no assumptions that objects of different types would be stored in the same area, etc.).

Upvotes: 3

Related Questions