Please check the content of my web page on 'A note on Pointer Operation'

L

Logan Lee

It is at http://211.30.198.135/pointer_operations.html. I want to get this reviewed to make sure that they are correct.

I'm not sure whether copy/paste will show up correctly but the content is:

A note on Pointer Operation

We start with the table:
Object Address
xp &xp
*p p

Table 1.

We can illustrate the relationship contained in the table clearer if we also visit the table:
Object Address Object Address
xp &xp *p p
*p p xp &xp

Table 2.

We can gather from this the following assignments:

xp=*p; &xp=p;

*p=xp; p=&xp;

We can immediately see from this that the relationship is commutative, namely that xp=*p and *p=xp or that &xp=p and p=&xp.

Also it occurs that &xp=&*p=p and &*p=&xp=p. Basically it is saying that &*p=p. What about *&xp? Yes, similarly we deduce that *&xp=*p=xp and *p=*&xp=xp, which means *&xp=xp. So we confirm that &xp=p and *p=xp.

Lastly, it also occurs that the operation &* is equivalent to *&.
 
L

Logan Lee

Logan Lee said:
It is at http://211.30.198.135/pointer_operations.html. I want to get this reviewed to make sure that they are correct.

I'm not sure whether copy/paste will show up correctly but the content is:

A note on Pointer Operation

We start with the table:
Object Address
xp &xp
*p p

Table 1.

We can illustrate the relationship contained in the table clearer if we also visit the table:
Object Address Object Address
xp &xp *p p
*p p xp &xp

Table 2.

We can gather from this the following assignments:
^^^^^^^^^^^
I'm not sure about the approriateness of the word 'assignments' here and also whether I should really write A==B rather than A=B.
 
L

Logan Lee

Logan Lee said:
It is at http://211.30.198.135/pointer_operations.html. I want to get this reviewed to make sure that they are correct.

I'm not sure whether copy/paste will show up correctly but the content is:

A note on Pointer Operation

We start with the table:
Object Address
xp &xp
*p p

Table 1.

We can illustrate the relationship contained in the table clearer if we also visit the table:
Object Address Object Address
xp &xp *p p
*p p xp &xp

Table 2.

We can gather from this the following assignments:

xp=*p; &xp=p;

*p=xp; p=&xp;

We can immediately see from this that the relationship is commutative, namely that xp=*p and *p=xp or that &xp=p and p=&xp.

Also it occurs that &xp=&*p=p and &*p=&xp=p. Basically it is saying that &*p=p. What about *&xp? Yes, similarly we deduce that *&xp=*p=xp and *p=*&xp=xp, which means *&xp=xp. So we confirm that &xp=p and *p=xp.

Lastly, it also occurs that the operation &* is equivalent to *&.

I can't set up apache very well so I put the stuff up on http://www.geocities.com/logan.lee30/pointer_operations.html.
 
W

William Pursell

Lastly, it also occurs that the operation &* is equivalent to *&.

In an academic sense, that may be true, but you need to be careful.
Consider:

char c = 'c';
'c' == *&c; /* okay */
'c' == &*c; /* error */

IOW, if you think of * and & as operators on the
appropriate space, then &* and *& are equivalent.
However, in an actual implementation, it aint so,
since the actual domains/codomains don't match.

Think of * as an operator on the space of all
objects of type pointer, and & as an operator
on the space of all objects. Clearly, *c
is undefined if c is not a pointer, so *&c
will be defined as long as c is an object,
but &*c is only defined if c is an object
of type pointer.
 
L

Logan Lee

William Pursell said:
is undefined if c is not a pointer, so *&c
will be defined as long as c is an object,
but &*c is only defined if c is an object
of type pointer.

OK.
 
P

Philip Potter

William said:
In an academic sense, that may be true, but you need to be careful.
Consider:

char c = 'c';
'c' == *&c; /* okay */
'c' == &*c; /* error */

IOW, if you think of * and & as operators on the
appropriate space, then &* and *& are equivalent.
However, in an actual implementation, it aint so,
since the actual domains/codomains don't match.

Think of * as an operator on the space of all
objects of type pointer,

values of type pointer. It doesn't have to be an object.
For example *(p + i) which is the same as the familiar p.

So another counterexample is the following:

char c = 'c';
char *pc = &c;
pc == &*&c; /* okay */
pc == *&&c; /* error */

....which shows that &* is sometimes valid when *& isn't.
 
P

Philip Potter

Logan said:
^^^^^^^^^^^
I'm not sure about the approriateness of the word 'assignments' here and also whether I should really write A==B rather than A=B.
I would agree entirely here. "Equalities" is probably a better word.
 
M

Mark McIntyre

Logan said:
^^^^^^^^^^^
I'm not sure about the approriateness of the word 'assignments' here and also whether I should really write A==B rather than A=B.

Assignments is correct.

double equals is very definitely wrong - thats the logical "is equal to"
operator, not the assignment operator.
 
W

William Pursell

In an academic sense, that may be true, but you need to be careful.
Consider:
char c = 'c';
'c' == *&c; /* okay */
'c' == &*c; /* error */
IOW, if you think of * and & as operators on the
appropriate space, then &* and *& are equivalent.
However, in an actual implementation, it aint so,
since the actual domains/codomains don't match.
Think of * as an operator on the space of all
objects of type pointer,

values of type pointer. It doesn't have to be an object.
For example *(p + i) which is the same as the familiar p.

So another counterexample is the following:

char c = 'c';
char *pc = &c;
pc == &*&c; /* okay */
pc == *&&c; /* error */

...which shows that &* is sometimes valid when *& isn't.


Is &* evident in that counterexample? The first
line is an example of the '&' operator being applied
to the result of *&c, while the second is in error
because of the '&&' construct. I am very confused,
however, about the following diagnostic:

$ cat a.c
#include <stdio.h>

int
main( void )
{
char c = 'c';
char **d;

d = &&c; /* Bogus code for demonstration.*/

return 0;
}

$ gcc a.c
a.c: In function 'main':
a.c:9: error: label 'c' used but not defined

Is this a gcc oddity?
 
W

Willem

William wrote:
) $ gcc a.c
) a.c: In function 'main':
) a.c:9: error: label 'c' used but not defined
)
) Is this a gcc oddity?

Probably. Since '&&' is an illegal construct in standard C, it can be used
as an extension. I would assume that in this case, '&&' operates on labels.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
P

Philip Potter

Mark said:
Assignments is correct.

double equals is very definitely wrong - thats the logical "is equal to"
operator, not the assignment operator.

Assignments describes the = operator correctly, but the = operator was
used wrongly in the original context. After making a statement, the OP
said "We can gather from this the following assignments" and suggested
that "the relationship is commutative" - suggesting that he was looking
at equalities in the code, rather than assignments, which are not
commutative.

Actually, looking at the OP again, he suggests that xp==*p without any
previous reference to a relationship between xp and p. This is not the
case. He should have first said p = &xp; although without stating the
type of p and xp it is not clear that this assignment is possible.

Philip
 
P

Philip Potter

William said:
William said:
Lastly, it also occurs that the operation &* is equivalent to *&.
In an academic sense, that may be true, but you need to be careful.
Consider:
char c = 'c';
'c' == *&c; /* okay */
'c' == &*c; /* error */
IOW, if you think of * and & as operators on the
appropriate space, then &* and *& are equivalent.
However, in an actual implementation, it aint so,
since the actual domains/codomains don't match.
Think of * as an operator on the space of all
objects of type pointer,
values of type pointer. It doesn't have to be an object.
For example *(p + i) which is the same as the familiar p.

So another counterexample is the following:

char c = 'c';
char *pc = &c;
pc == &*&c; /* okay */
pc == *&&c; /* error */

...which shows that &* is sometimes valid when *& isn't.


Is &* evident in that counterexample? The first
line is an example of the '&' operator being applied
to the result of *&c, while the second is in error
because of the '&&' construct.


Replace '&c' with any pointer value with no address:

register char *c;
&*c; /* okay [1] */
*&c; /* error */

Philip

[1] and even though this looks like it dereferences an uninitialized
pointer, which is a Bad Thing, this particular example is defined
behaviour due to the special case in 6.5.3.2p3.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top