This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Difference between two pointers

I'm using the ARM Realview compiler with a Cortex-M3 device. I would like to caclculate the difference/distance between two pointers:

char* IndexStart, IndexStop;

// Search for the beginning of the domain name
IndexStart = strchr(((char*)(ÐRxBuffer[0])), ' ');
// Search for the end of the domain name
IndexStart = strchr(++IndexStart, '\r');
// Copy the domain name
(void)strncpy((char*)SMTPDomainName, IndexStart, (IndexStop - IndexStart));

But I get an error message: error: #32: expression must have arithmetic type when subtracting the pointer and using the result as an integer type.

According to the ARM documentation this is not allowed by the ISO C standard. I've disabled strict mode by setting --no_strict even though --no_strict is the default compiler setting but it still gives me this error.

How can I work around this?

Parents
  • You can avoid the "problem" by the simple expedient of only ever allowing one declaration per statement.

    This has a couple of added benefits:

    1. Makes it slightly easier if you need to change the type of a variable;

    2. Allows (encourages?) you to comment each declaration; eg,

    char *IndexStart; // Pointer to the beginning of the domain name string
    char *IndexStop;  // Pointer to the end       of the domain name string
    

    BTW: it's misleading to name your variables "Index" when they are actually pointers!

    BTW2: The comment could be more explicit in what it actually means by the "end" of the string...

Reply
  • You can avoid the "problem" by the simple expedient of only ever allowing one declaration per statement.

    This has a couple of added benefits:

    1. Makes it slightly easier if you need to change the type of a variable;

    2. Allows (encourages?) you to comment each declaration; eg,

    char *IndexStart; // Pointer to the beginning of the domain name string
    char *IndexStop;  // Pointer to the end       of the domain name string
    

    BTW: it's misleading to name your variables "Index" when they are actually pointers!

    BTW2: The comment could be more explicit in what it actually means by the "end" of the string...

Children