Hi,
I am subtracting two pointers to same function.
Like
int th(int arg)
{
return 3*arg;
}

and

int (*p1)(int)=&th;
int (*p22)(int)=&th;

now when I do p1-p2 it gives error !!!
-' : illegal, left operand has type 'int (__cdecl *)(int)'

however in case of pointers to any other data type it works...

Why ?
And how can i solve it
Thanks..
a.a.cpp

Re: subtracting two pointers to same function ? by John

John
Thu Apr 21 06:18:11 CDT 2005

"IceColdFire" <IceColdFire@discussions.microsoft.com> wrote in message
news:A1CCAB6D-E93A-499B-9E50-5C73AEE1D638@microsoft.com
> Hi,
> I am subtracting two pointers to same function.

Why?

> Like
> int th(int arg)
> {
> return 3*arg;
> }
>
> and
>
> int (*p1)(int)=&th;
> int (*p22)(int)=&th;
>
> now when I do p1-p2 it gives error !!!

You defined p22 and now you are talking about p2.

If you want to check equality, then p1 == p2 will compile.

>
> however in case of pointers to any other data type it works...
>
> Why ?

You can't perform pointer arithmetic with function pointers.

Pointer arithmetic is designed for arrays. You can have an array of ints and
hence pointer arithmetic is defined for pointers to int. You CANNOT have
arrays of FUNCTIONS and hence pointer arithmetic is not defined for pointers
to functions.

What you can have is an array of POINTERS to functions. Thus pointer
arithmetic is defined for pointers to pointers to functions. The following
compiles:

int th(int arg)
{
return 3*arg;
}


int (*p1)(int)=&th;
int (*p2)(int)=&th;

int (**pp1)(int)=&p1;
int (**pp2)(int)=&p2;

int main()
{
int x = pp1-pp2;
return 0;
}

x is not zero, however, since p1 and p2 are distinct variables with distinct
addresses. As already stated, to check for equality of p1 and p2, you should
test p1 == p2.


--
John Carson


Re: subtracting two pointers to same function ? by John

John
Thu Apr 21 06:30:14 CDT 2005

As an addendum: Given

T * p1, *p2;

p1-p2 is the difference in the addresses pointed to by p1 and p2 divided by
sizeof(T). Since functions don't have sizes in the way that types do, it
follows that pointer differences aren't defined for pointers to functions.

--
John Carson


Re: subtracting two pointers to same function ? by IceColdFire

IceColdFire
Thu Apr 21 06:42:02 CDT 2005

Hi John,

Thank you so much for the solution...your addendum did the real magic and
cleared all doubts...
thanks again..

however I am still confused with diff of pointers to functions although your
addendum clearly explains the issue....

How can it be that standard define such diff of pointers but compilers dont
implement them...
is it possible...

Thanks
a.a.cpp

"John Carson" wrote:

> "IceColdFire" <IceColdFire@discussions.microsoft.com> wrote in message
> news:A1CCAB6D-E93A-499B-9E50-5C73AEE1D638@microsoft.com
> > Hi,
> > I am subtracting two pointers to same function.
>
> Why?
>
> > Like
> > int th(int arg)
> > {
> > return 3*arg;
> > }
> >
> > and
> >
> > int (*p1)(int)=&th;
> > int (*p22)(int)=&th;
> >
> > now when I do p1-p2 it gives error !!!
>
> You defined p22 and now you are talking about p2.
>
> If you want to check equality, then p1 == p2 will compile.
>
> >
> > however in case of pointers to any other data type it works...
> >
> > Why ?
>
> You can't perform pointer arithmetic with function pointers.
>
> Pointer arithmetic is designed for arrays. You can have an array of ints and
> hence pointer arithmetic is defined for pointers to int. You CANNOT have
> arrays of FUNCTIONS and hence pointer arithmetic is not defined for pointers
> to functions.
>
> What you can have is an array of POINTERS to functions. Thus pointer
> arithmetic is defined for pointers to pointers to functions. The following
> compiles:
>
> int th(int arg)
> {
> return 3*arg;
> }
>
>
> int (*p1)(int)=&th;
> int (*p2)(int)=&th;
>
> int (**pp1)(int)=&p1;
> int (**pp2)(int)=&p2;
>
> int main()
> {
> int x = pp1-pp2;
> return 0;
> }
>
> x is not zero, however, since p1 and p2 are distinct variables with distinct
> addresses. As already stated, to check for equality of p1 and p2, you should
> test p1 == p2.
>
>
> --
> John Carson
>
>

Re: subtracting two pointers to same function ? by Victor

Victor
Thu Apr 21 08:28:48 CDT 2005

IceColdFire wrote:
> [...]
> however I am still confused with diff of pointers to functions although your
> addendum clearly explains the issue....
>
> How can it be that standard define such diff of pointers but compilers dont
> implement them...
> is it possible...

No, it's not possible. The additive (+ or -) operations are only defined
for pointers to objects. Since pointers to functions are not pointers to
objects, your question cannot be answered. The Standard does not define
p1-p2 when 'p1' or 'p2' is a pointer to a function. See sub-clause 5.7,
paragraph 2.

V

Re: subtracting two pointers to same function ? by IceColdFire

IceColdFire
Thu Apr 21 22:35:02 CDT 2005

Hi,

Indeed...your explanation is correct...However here is the snippet from the
C++ standard clause Point 8, as

"If the value 0 is added to or subtracted from a pointer value, the result
compares equal to the original"pointer value. If two pointers point to the
same object or function or both point one past the end of the"same array or
both are null, and the two pointers are subtracted, the result compares equal
to the value 0"converted to the type ptrdiff_t."

the 3rd line in above para says 'function'....this issue has been too
complex....
Kindly suggest.

Thanks.
a.a.cpp

"Victor Bazarov" wrote:

> IceColdFire wrote:
> > [...]
> > however I am still confused with diff of pointers to functions although your
> > addendum clearly explains the issue....
> >
> > How can it be that standard define such diff of pointers but compilers dont
> > implement them...
> > is it possible...
>
> No, it's not possible. The additive (+ or -) operations are only defined
> for pointers to objects. Since pointers to functions are not pointers to
> objects, your question cannot be answered. The Standard does not define
> p1-p2 when 'p1' or 'p2' is a pointer to a function. See sub-clause 5.7,
> paragraph 2.
>
> V
>

Re: subtracting two pointers to same function ? by Victor

Victor
Thu Apr 21 23:06:23 CDT 2005

IceColdFire wrote:
> Indeed...your explanation is correct...However here is the snippet
> from the C++ standard clause Point 8, as
>
> "If the value 0 is added to or subtracted from a pointer value, the
> result compares equal to the original"pointer value. If two pointers
> point to the same object or function or both point one past the end
> of the"same array or both are null, and the two pointers are
> subtracted, the result compares equal to the value 0"converted to the
> type ptrdiff_t."
>
> the 3rd line in above para says 'function'....this issue has been too
> complex....
> Kindly suggest.

First of all, it's "two pointers to the _same_ function", so it's a bit
different from your original premise of "two pointers to functionS".

Second, if the Standard contains two statements, one direct (in our
case allowing only pointers to objects to participate in additive ops)
and another one indirect, then the direct one precedes.

Third, it has been already reported as a defect (issue 179) and resolved
by removing "or functions" in TC1. I guess this makes the 'first' and
the 'second' irrelevant.

V


P.S. Please don't top-post. Thanks.



Re: subtracting two pointers to same function ? by John

John
Thu Apr 21 23:54:06 CDT 2005

"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:%23WRMmCvRFHA.1348@TK2MSFTNGP15.phx.gbl
>
> Third, it has been already reported as a defect (issue 179) and
> resolved by removing "or functions" in TC1. I guess this makes the
> 'first' and the 'second' irrelevant.
>

Just to be completely explicit, the "or functions" has already been removed
from the standard. It exists in the 1998 standard but not in the 2003
standard.

--
John Carson


Re: subtracting two pointers to same function ? by IceColdFire

IceColdFire
Fri Apr 22 01:38:02 CDT 2005

Hi,
Thanks a lot...
I appreciate the solutions provided to the problem...indeed the issue was
handled quite well..th eproblem has been resolved as by removing 'or
funcitons' as in new standard...

thanks again
a.a.cpp

"John Carson" wrote:

> As an addendum: Given
>
> T * p1, *p2;
>
> p1-p2 is the difference in the addresses pointed to by p1 and p2 divided by
> sizeof(T). Since functions don't have sizes in the way that types do, it
> follows that pointer differences aren't defined for pointers to functions.
>
> --
> John Carson
>
>