a pointer that points to itself

P

pete

Robert said:
ITYM "pointer is %p\n"

No. I was emphasizing the fact that they compare equal.
The obvious example, however it is quite debatable (and has been
debated very much so, no need to start again now) as to whether
"pointer" *points* to anything at all (being a pointer to void).

The non-NULL return value of malloc points to memory.
The NULL return value of malloc, doesn't.
 
K

Keith Thompson

pete said:
Robert Gamble wrote: [...]
ITYM "pointer is %p\n"

No. I was emphasizing the fact that they compare equal.

You wrote:

void *pointer = &pointer;

if (pointer == &pointer) {
printf("&pointer is %p\n", pointer);
printf("&pointer is %p\n", (void *)&pointer);
}

I think what you meant was:

void *pointer = &pointer;

if (pointer == &pointer) {
printf("pointer is %p\n", pointer);
printf("&pointer is %p\n", (void *)&pointer);
}
 
P

pete

Keith said:
pete said:
Robert Gamble wrote: [...]
ITYM "pointer is %p\n"

No. I was emphasizing the fact that they compare equal.

You wrote:

void *pointer = &pointer;

if (pointer == &pointer) {
printf("&pointer is %p\n", pointer);
printf("&pointer is %p\n", (void *)&pointer);
}

I think what you meant was:

void *pointer = &pointer;

if (pointer == &pointer) {
printf("pointer is %p\n", pointer);
printf("&pointer is %p\n", (void *)&pointer);
}

(pointer) and ((void *)&pointer) are two expressions
which have the exact same type and value,
therefore they are interchangable as arguments
in a function call.
 
W

Walter Roberson

(pointer) and ((void *)&pointer) are two expressions
which have the exact same type and value,
therefore they are interchangable as arguments
in a function call.

Pointers can have different sizes. A pointer to a char
is not necessarily the same size as a pointer to an int,
for example -- and from what I've read in other postings,
there have been real implementations in which they were
quite different.

Look at all the verbage the standard spends on talking about
-converting- pointer types (which it distinguishes from
removing type qualifications.)
 
K

Keith Thompson

pete said:
Keith said:
pete said:
Robert Gamble wrote: [...]
ITYM "pointer is %p\n"

No. I was emphasizing the fact that they compare equal.

You wrote:

void *pointer = &pointer;

if (pointer == &pointer) {
printf("&pointer is %p\n", pointer);
printf("&pointer is %p\n", (void *)&pointer);
}

I think what you meant was:

void *pointer = &pointer;

if (pointer == &pointer) {
printf("pointer is %p\n", pointer);
printf("&pointer is %p\n", (void *)&pointer);
}

(pointer) and ((void *)&pointer) are two expressions
which have the exact same type and value,
therefore they are interchangable as arguments
in a function call.

Yes, but by printing "&pointer is ..." on both lines, you're
*asserting* that they're the same thing, not demonstrating it. The
output of your program doesn't demonstrate the point you're trying to
make.
 
F

Flash Gordon

Walter said:
Pointers can have different sizes. A pointer to a char
is not necessarily the same size as a pointer to an int,
for example -- and from what I've read in other postings,
there have been real implementations in which they were
quite different.

Look at all the verbage the standard spends on talking about
-converting- pointer types (which it distinguishes from
removing type qualifications.)

In general you are correct, but in the message pete was replying to
pointer was declared with the line
| void *pointer = &pointer;
making peter's statement correct in *this* instance, just not in the
general case.
pointer is of type void* and contains the address of pointer. &pointer
is obviously the address of pointer, and after the cast the value has
been converted to type void* just as it was when pointer was initialised.
 
C

Chris Torek

Keith Thompson wrote:
[snippage]
printf("&pointer is %p\n", pointer); [should have read]
printf("pointer is %p\n", pointer);

(pointer) and ((void *)&pointer) are two expressions
which have the exact same type and value,
therefore they are interchangable as arguments
in a function call.

Keith's, er, "point", is not that the second argument to printf()
is wrong, but that the first one -- the string -- prints something
other than what he think you meant to print. It might be
more obvious if the first line read:

printf("Mozambique Itemize Sasquatch! %p!!\n", pointer);

and the second then read:

printf("pointer is %p\n", pointer);

:)
 
P

pete

Keith said:
pete said:
Keith said:
Robert Gamble wrote:
[...]
ITYM "pointer is %p\n"

No. I was emphasizing the fact that they compare equal.

You wrote:

void *pointer = &pointer;

if (pointer == &pointer) {
printf("&pointer is %p\n", pointer);
printf("&pointer is %p\n", (void *)&pointer);
}

I think what you meant was:

void *pointer = &pointer;

if (pointer == &pointer) {
printf("pointer is %p\n", pointer);
printf("&pointer is %p\n", (void *)&pointer);
}

(pointer) and ((void *)&pointer) are two expressions
which have the exact same type and value,
therefore they are interchangable as arguments
in a function call.

Yes, but by printing "&pointer is ..." on both lines, you're
*asserting* that they're the same thing, not demonstrating it.

Yes, that's my assertion.
 
P

pete

Flash said:
In general you are correct, but in the message pete was replying to
pointer was declared with the line
| void *pointer = &pointer;
making peter's statement correct in *this* instance,
just not in the general case.
pointer is of type void* and contains the address of pointer.
&pointer is obviously the address of pointer,
and after the cast the value has been converted to type void*
just as it was when pointer was initialised.

That's it.
 
K

Keith Thompson

pete said:
Keith said:
pete said:
Keith Thompson wrote:

Robert Gamble wrote:
[...]
ITYM "pointer is %p\n"

No. I was emphasizing the fact that they compare equal.

You wrote:

void *pointer = &pointer;

if (pointer == &pointer) {
printf("&pointer is %p\n", pointer);
printf("&pointer is %p\n", (void *)&pointer);
}

I think what you meant was:

void *pointer = &pointer;

if (pointer == &pointer) {
printf("pointer is %p\n", pointer);
printf("&pointer is %p\n", (void *)&pointer);
}

(pointer) and ((void *)&pointer) are two expressions
which have the exact same type and value,
therefore they are interchangable as arguments
in a function call.

Yes, but by printing "&pointer is ..." on both lines, you're
*asserting* that they're the same thing, not demonstrating it.

Yes, that's my assertion.

Sigh. I give up.
 
P

pete

Chris said:
Keith Thompson wrote: [snippage]
printf("&pointer is %p\n", pointer); [should have read]
printf("pointer is %p\n", pointer);

(pointer) and ((void *)&pointer) are two expressions
which have the exact same type and value,
therefore they are interchangable as arguments
in a function call.

Keith's, er, "point", is not that the second argument to printf()
is wrong, but that the first one -- the string -- prints something
other than what he think you meant to print. It might be
more obvious if the first line read:

printf("Mozambique Itemize Sasquatch! %p!!\n", pointer);

and the second then read:

printf("pointer is %p\n", pointer);

:)

My point is that

printf("&pointer is %p\n", pointer);

is a valid way to print the value of &pointer,
given that (pointer == &pointer) is true.

In some contexts, such as with the equality operator, and %p,
there is no difference between pointer and &pointer,
even though they are expressions of different types.

When (pointer == &pointer), then pointer points to itself
as much as the return value of malloc can point to anything.
 
T

tanvir

I suspect that 'dough' means "in a single statement". The use of it
would be to answer homework questions.

I have a solution that compiles and executes without problem with
any warning level I throw at it. It does raise some subtle language
points.
 
D

Dave Thompson

Actually, I did once use it as the boundary case for a type-unsafely
generic linked list. I'd call that _very little_ real use, however.
- David.Thompson1 at worldnet.att.net
 
J

Jordan Abel

I suspect that 'dough' means "in a single statement". The use of it
would be to answer homework questions.

I have a solution that compiles and executes without problem with
any warning level I throw at it. It does raise some subtle language
points.

I'm not sure what's so subtle about void *p = &p; - or is this wrong and
you had something else in mind?
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top