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?
No way! Thanks for that!
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...
when I take over a project, the first I do is to split all "multistatement" lines.
it makes the code readable, breakpointable, commenteable, ....able.
I know that some take pride in cramming the most into one line, they can take that pride and ....
Erik
Ditto.
It normally seems to be done by people who think they know (as opposed to really know) what the compiler does. The most frequent reason given in our reviews is that they believe it will make their code faster and more efficient.
Poppycock!
"Poppycock!"
To put it politely!
The most frequent reason given in our reviews is that they believe it will make their code faster and more efficient
LOL X 10000 :-)
One variable/line means a change to a variable type changes one (1) source code line affecting one (1) variable. So way easier to read a diff between two versions at a later time.
I want code fixes to be minimalistic.