Synopsis: | Pointers shall not be subtracted from each other, unless their unqualified type is the same. |
Language: | C |
Severity Level: | 2 |
Category: | Conversions |
Description: |
Justification Pointer subtraction only has meaning for determining an offset. Note The unqualified type of a pointer is its type without "const", "volatile", etc. Example char *p; char *q; float *f; static const char message[] = "This is a message for ..."; float g = 0.0; p = &message[5]; q = &message[10]; f = &g; printf("Number of elements: %d\n", q - p); /* RIGHT */ printf("Number of elements: %d\n", q - f); /* WRONG */ |