Keil Logo Arm Logo

Difference between two pointers

Next Thread | Thread List | Previous Thread Start a Thread | Settings

Details Message
Read-Only
Author
MC Potgieter
Posted
31-Mar-2012 09:56 GMT
Toolset
ARM
New! 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?

Read-Only
Author
Andrew Neil
Posted
31-Mar-2012 09:58 GMT
Toolset
None
New! You only have *one* pointer!
char* IndexStart, IndexStop;


http://c-faq.com/decl/charstarws.html


Read-Only
Author
MC Potgieter
Posted
31-Mar-2012 10:08 GMT
Toolset
None
New! RE: You only have *one* pointer!

No way! Thanks for that!

Read-Only
Author
Andrew Neil
Posted
31-Mar-2012 11:19 GMT
Toolset
None
New! RE: You only have *one* pointer!

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...

Read-Only
Author
Erik Malund
Posted
31-Mar-2012 21:23 GMT
Toolset
None
New! when I take over a project, the first I do is ...

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

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

Read-Only
Author
S Smyth
Posted
31-Mar-2012 21:33 GMT
Toolset
None
New! RE: when I take over a project, the first I do is ...

when I take over a project, the first I do is to split all "multistatement" lines.

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!

Read-Only
Author
Andrew Neil
Posted
1-Apr-2012 08:26 GMT
Toolset
None
New! RE: they believe it will make their code faster and more efficient.

"Poppycock!"

To put it politely!

Read-Only
Author
Tamir Michael
Posted
1-Apr-2012 13:34 GMT
Toolset
None
New! RE: they believe it will make their code faster and more efficient.

The most frequent reason given in our reviews is that they believe it will make their code faster and more efficient

LOL X 10000 :-)

Read-Only
Author
Per Westermark
Posted
1-Apr-2012 15:11 GMT
Toolset
None
New! RE: they believe it will make their code faster and more efficient.

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.

Next Thread | Thread List | Previous Thread Start a Thread | Settings

Keil logo

Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.