Re: C ethics question




"Servé Laurijssen" <ser@xxxx> wrote in message
news:eulh3t$vre$1@xxxxxxxxxxxxxxxxxxxxxxxxxx

"Harald van D?k" <truedfx@xxxxxxxxx> wrote in message
news:1175340222.133195.49390@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Servé Laurijssen wrote:
Recently, I found myself in the following situation:

There is library software written in C which declares some externals
like:

struct METER m1;
struct METER m2;

in different sourcefiles and I have no control over these sources.
Then there's functions that operate on these meters and will generate an
event which passes a METER * to identify which meter has been changed.

static void OnChangemeter(struct METER *m)
{
if (m == &m1) ...
if (m == &m2) ...
}

Now the comparison of these pointers is UB in C [...]

No, it isn't.

oh, I thought that you can only compare pointers if they are declared in
the same object.

struct METER meters[10];

if (m == &m[1]) ..
etc would have worked but comparing "random" pointers is UB.




You may be confusing this with the behavior of <,>,<=,>=.

Read the standard sections on relational operators and equality
operators.


.



Relevant Pages

  • Re: C ethics question
    ... There is library software written in C which declares some externals like: ... struct METER m1; ... in different sourcefiles and I have no control over these sources. ...
    (comp.lang.c)
  • Re: C ethics question
    ... struct METER m1; ... in different sourcefiles and I have no control over these sources. ... pointer to an object ...
    (comp.lang.c)
  • C ethics question
    ... There is library software written in C which declares some externals like: ... struct METER m1; ... in different sourcefiles and I have no control over these sources. ...
    (comp.lang.c)
  • Re: C ethics question
    ... There is library software written in C which declares some externals like: ... struct METER m1; ... in different sourcefiles and I have no control over these sources. ...
    (comp.lang.c)