473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

a pointer that points to itself

Is it possible in C to declare and initialize a pointer that points to
itself? Why or why not?

Nov 15 '05 #1
33 4824

dough wrote:
Is it possible in C to declare and initialize a pointer that points to
itself? Why or why not?


Sure, but of what use would it be?

Nov 15 '05 #2
In article <11**********************@g44g2000cwa.googlegroups .com>,
tedu <tu@zeitbombe.org> wrote:
dough wrote:
Is it possible in C to declare and initialize a pointer that points to
itself? Why or why not?
Sure, but of what use would it be?


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.

--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
Nov 15 '05 #3
Walter Roberson wrote:
tedu <tu@zeitbombe.org> wrote:
dough wrote:
Is it possible in C to declare and initialize a pointer that points
to itself? Why or why not?
Sure, but of what use would it be?


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.


I suspect it's done so rarely that compiler writers can't justify the
time and code needed to detect the questionable code.
It does raise some subtle language points.


A generic circular list containing 1 element may well have a pointer
that
points to itself. If you're talking about expressions like...

p = ((cast) p)->next;

....then this has been discussed before in csc.

--
Peter

Nov 15 '05 #4
Walter Roberson wrote:
In article <11**********************@g44g2000cwa.googlegroups .com>,
tedu <tu@zeitbombe.org> wrote:

dough wrote:
Is it possible in C to declare and initialize a pointer that points to
itself? Why or why not?


Sure, but of what use would it be?

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.

The point it raises is this: may an initializer for a variable reference
the address of that variable?

The point is fairly academic, since it's hard to imagine a situation
where this is both useful and not rewritable as initialization after
declaration. Note that we're not talking about nonsense like
int j = j + 1;
which is undefined in any case since j is used uninitialized. But how about
ptrdiff_t y = &y - &y;

Another poorly conceived homework question, then -- somehow these things
always ask students to invoke undefined behaviour or technically
require them to find out whether they will.

S.
Nov 15 '05 #5
I am confused. Why would you think it is undefined? The scope of a
variable starts immediately after the declarator is done, it has
nothing to do with initialization. int j = j + 1 and int j[j] are
undefined due to different reasons; in fact, one can contrive
situations where the latter statement is completely well-defined.

Nov 15 '05 #6
tedu wrote:
dough wrote:
Is it possible in C to declare and initialize a pointer that points to
itself? Why or why not?

Sure, but of what use would it be?

actually no. The types would be incompatible.
You can't do int *foo = &foo; &foo has type int**.
Nov 15 '05 #7
Nils O. Selåsdal wrote:
tedu wrote:
dough wrote:
Is it possible in C to declare and initialize a pointer that points
to itself? Why or why not?


Sure, but of what use would it be?


actually no. The types would be incompatible.
You can't do int *foo = &foo; &foo has type int**.


But you can change int for another type which makes it work.

--
Peter

Nov 15 '05 #8
On 28 Sep 2005 22:22:27 -0700,
ta*********@gmail.com <ta*********@gmail.com> wrote:

I am confused. Why would you think it is undefined? The scope of a
variable starts immediately after the declarator is done, it has
nothing to do with initialization. int j = j + 1 and int j[j] are
undefined due to different reasons; in fact, one can contrive
situations where the latter statement is completely well-defined.

"After the declarator", is that at the following semicolon or comma,
or immediately after the name itself but before a possible equals sign?
Villy
Nov 15 '05 #9
ta*********@gmail.com writes:
I am confused. Why would you think it is undefined? The scope of a
variable starts immediately after the declarator is done, it has
nothing to do with initialization. int j = j + 1 and int j[j] are
undefined due to different reasons; in fact, one can contrive
situations where the latter statement is completely well-defined.


It's undefined because we don't know what you're talking about. You
need to provide context when you post a followup; we can't necessarily
see the article to which you're replying.

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

This advice has been posted here well over 1000 times.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #10
Nils O. Selåsdal wrote:
tedu wrote:
dough wrote:
Is it possible in C to declare and initialize a pointer that points to
itself? Why or why not?


Sure, but of what use would it be?


actually no. The types would be incompatible.
You can't do int *foo = &foo; &foo has type int**.


Actually, if you pick the correct types, you don't have a problem with
types and pointers to pointers.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #11

dough wrote:
Is it possible in C to declare and initialize a pointer that points to
itself? Why or why not?

char * a = (char*) &a;

Nov 15 '05 #12
Flash Gordon wrote:

Nils O. Selåsdal wrote:
tedu wrote:
dough wrote:

Is it possible in C to declare and initialize a pointer that points to
itself? Why or why not?

Sure, but of what use would it be?


actually no. The types would be incompatible.
You can't do int *foo = &foo; &foo has type int**.


Actually, if you pick the correct types, you don't have a problem with
types and pointers to pointers.


/* BEGIN new.c */

#include <stdio.h>

int main(void)
{
void *pointer = &pointer;

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

/* END new.c */
--
pete
Nov 15 '05 #13
ta*********@gmail.com wrote:
Skarmander restored context when he copied:
The point it raises is this: may an initializer for a variable
reference the address of that variable?

The point is fairly academic, since it's hard to imagine a
situation where this is both useful and not rewritable as
initialization after declaration. Note that we're not talking about
nonsense like

int j = j + 1;

which is undefined in any case since j is used uninitialized. But
how about

ptrdiff_t y = &y - &y;

I am confused. Why would you think it is undefined? The scope of a
variable starts immediately after the declarator is done, it has
nothing to do with initialization. int j = j + 1 and int j[j] are
undefined due to different reasons; in fact, one can contrive
situations where the latter statement is completely well-defined.

I know
int j = j + 1;
is undefined for a different reason. I explicitly said it wasn't what we
were talking about (but now we are :-)

I also know that in turn is completely different from
int j[j];
which involves getting tricky with overloading. This is unlike
everything else we've discussed because there are two distinct 'j's here.

This still doesn't answer my question, because that asks about the scope
of a variable *while in the initialization part of the declaration*. If
you happen to know that's defined, great. Quote the relevant parts of
the standard that imply this, please. (But like I said, the point is
fairly academic, so you won't see me crying if this is forever left
unresolved.)

S.
Nov 15 '05 #14
On Wed, 28 Sep 2005 23:28:07 -0700, Peter Nilsson wrote:
Nils O. Selåsdal wrote:
tedu wrote:
> dough wrote:
> > Is it possible in C to declare and initialize a pointer that points
> > to itself? Why or why not?
>
> Sure, but of what use would it be?


actually no. The types would be incompatible.
You can't do int *foo = &foo; &foo has type int**.


But you can change int for another type which makes it work.


However there is no type for which this works. If your pointer have type
TYPE * then a pointer to it has type TYPE **. Since an object in C can
only have one type it can't be both TYPE * and TYPE ** at once.

However there is a similar question for which C does have an answer: "Is
it possible in C to declare and initialize a pointer that points to
THE FIRST BYTE of itself?"

That can be done by for example

char *ptr = (char *)&ptr;

or perhaps

void *ptr = &ptr;

A pointer to the pointer object can be recreated from these by converting
the value back to the correct type (char ** and void ** respectively).

Lawrence

Nov 15 '05 #15
Skarmander <in*****@dontmailme.com> wrote:
ta*********@gmail.com wrote:
Skarmander restored context when he copied:
The point it raises is this: may an initializer for a variable
reference the address of that variable?

The point is fairly academic, since it's hard to imagine a
situation where this is both useful and not rewritable as
initialization after declaration. Note that we're not talking about
nonsense like

int j = j + 1;

which is undefined in any case since j is used uninitialized. But
how about

ptrdiff_t y = &y - &y;

I am confused. Why would you think it is undefined? The scope of a
variable starts immediately after the declarator is done, it has
nothing to do with initialization. int j = j + 1 and int j[j] are
undefined due to different reasons; in fact, one can contrive
situations where the latter statement is completely well-defined.

I know
int j = j + 1;
is undefined for a different reason. I explicitly said it wasn't what we
were talking about (but now we are :-)

I also know that in turn is completely different from
int j[j];
which involves getting tricky with overloading. This is unlike
everything else we've discussed because there are two distinct 'j's here.

This still doesn't answer my question, because that asks about the scope
of a variable *while in the initialization part of the declaration*. If
you happen to know that's defined, great. Quote the relevant parts of
the standard that imply this, please. (But like I said, the point is
fairly academic, so you won't see me crying if this is forever left
unresolved.)


6.2.1#7. Initializer is not part of a declarator.
Some useful examples are in C++ Standard.

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 15 '05 #16
A small example to understand "pointer points to itself"

int main()
{
char *p = "xyz";

printf(" P:%s &P:%p \n", p , &p);

p = &p;

printf(" P:%p &P:%p \n", p , &p);
return 0;
}

result: P: xyz &P: 0xbfffec44
P: 0xbfffec44 &P: 0xbfffec44

Nov 15 '05 #17
Lawrence Kirby wrote:
On Wed, 28 Sep 2005 23:28:07 -0700, Peter Nilsson wrote:

Nils O. Selåsdal wrote:
tedu wrote:

dough wrote:

>Is it possible in C to declare and initialize a pointer that points
>to itself? Why or why not?

Sure, but of what use would it be?

actually no. The types would be incompatible.
You can't do int *foo = &foo; &foo has type int**.


But you can change int for another type which makes it work.


However there is no type for which this works. If your pointer have type
TYPE * then a pointer to it has type TYPE **. Since an object in C can
only have one type it can't be both TYPE * and TYPE ** at once.

However there is a similar question for which C does have an answer: "Is
it possible in C to declare and initialize a pointer that points to
THE FIRST BYTE of itself?"

That can be done by for example

char *ptr = (char *)&ptr;

or perhaps

void *ptr = &ptr;

A pointer to the pointer object can be recreated from these by converting
the value back to the correct type (char ** and void ** respectively).


It was this last example I was thinking of. Personally I would consider
it as pointing to the object although I do accept that it needs to be
converted back before it is used.

Or you could use a struct:
struct ltyp {
struct ltyp *node;
};
struct ltyp ltyp_ptr = { &ltyp_ptr };

So part of the struct is initialised as a pointer to the struct, which
is the same as a pointer to the first element of the struct.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #18
pete wrote:
Flash Gordon wrote:

Nils O. Selåsdal wrote:
tedu wrote:

> dough wrote:
>
>> Is it possible in C to declare and initialize a pointer that pointsto
>> itself? Why or why not?
>
> Sure, but of what use would it be?

actually no. The types would be incompatible.
You can't do int *foo = &foo; &foo has type int**.
Actually, if you pick the correct types, you don't have a problem with
types and pointers to pointers.


/* BEGIN new.c */

#include <stdio.h>

int main(void)
{
void *pointer = &pointer;

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


ITYM "pointer is %p\n"
printf("&pointer is %p\n", (void *)&pointer);
}
return 0;
}

/* END new.c */


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

Robert Gamble

Nov 15 '05 #19
Villy Kruse wrote:
"After the declarator", is that at the following semicolon or comma,
or immediately after the name itself but before a possible equals sign?


The declarator is whatever declares the identifier. One can get
formal, but it is easier to say that the declarator ends whenever the
parts to do with its type (not value or implementation; for things
other than stuff that refers to objects and functions, e.g. tags and
labels, there is no conceptual issue) are done. Some examples are
probably easier. In char z[3] = {0,0,0}, x; the scope of x starts
after x and of z after the [3]. The initializer is not part of the
declarator, nor are independent declarations following it. I do hate
to use terms like before a possible = sign because that is a bad way to
think: if you write int f (enum {a = 0, b = 1}); the scope of f still
starts after the closing parenthesis, even though there are equal signs
before it (The example is bad, but not incorrect). I know you meant
the = sign that gives it its initial value, but since C is a strongly
typed language, it is much easier to think of the distintion between
type and value than low level lexical analysis.

There is an aspect that makes the informal discussion above incomplete:
one could argue that after the closing bracket in char a[] = "abc", the
type of a is not yet known, it is only after initialization that we
know it is char[4]. This is a special case because of all incomplete
types only incomplete array types can actually complete them allowing
the apparent definition of objects of such types. The formal rules
specify that even in this case, the declarator ends at the closing
bracket. Because of this you could argue that we should always be
formal, but, at least in C, the formalism tries to capture a
distinction that exists in most programmers' mind; and, it comes out
naturally in explanations.

It is for this reason in my previous post I was contrasting int j = j +
1; versus int j[j]. In the first case, the second j is after the first
j has come in scope and evaluates it. Since the j does not have a
value at this stage, the evaluation is undefined. In int j[j], by
contrast, the scope of the first j has not yet begun when the second j
is used, so it refers to something else altogether! It is usually
undefined, not because j is evaluated before it is given a value, nor
because the array dimension does not have an integral type, but rather
because it refers to an identifier which does not exist! If you write,
for example,
enum {j = 3}; {int j[j];}
then the int j[j] is perfectly well defined and means int j[3]. What
happened is the new j has not yet come into scope, but the scope of the
previous j has not ended, so we just get that meaning of j. Note the
scope rules: the very similar example
enum {j = 3}; {int j = j + 1; }
is truly undefined; you do not get the effect of int j = 4 (or rather,
since it is undefined, anything, including this, might happen) because
the initializer is in the scope of the new j and the old j is hidden.

So what has this got to do with examples like whether
ptrdiff_t d = &d - &d;
is undefined? Well, let us see why it could be undefined. As the
second example with the construction `int j = j + 1' shows the j in the
initializer refers to the newly defined j, similarly, the new d should
refer to the newly defined d, the one without a value yet. Somebody
has already provided a quotation from the standard, and I do not go
into that.

Next we need to know whether we can evaluate &d when d does not have a
value. The answer to this was not doubted by anyone in the discussion,
think {int x; ptrdiff_t d = &x - &x;}, though one could again ask about
quotation from the standard. Similar questions about the validity of
the subtraction and the initialization are actually completely explicit
in the standard. So, I was pointing out that there was no reason to
suspect undefined behaviour: or at least, none that I could see.

I apologize for my previous post being sans context. The admonition
not to use google default must have slipped by me in my careless quick
perusal.

Nov 15 '05 #20
Robert Gamble wrote:

pete wrote:
Flash Gordon wrote:

Nils O. Selåsdal wrote:
> tedu wrote:
>
>> dough wrote:
>>
>>> Is it possible in C to declare and initialize a pointer that points to
>>> itself? Why or why not?
>>
>> Sure, but of what use would it be?
>
> actually no. The types would be incompatible.
> You can't do int *foo = &foo; &foo has type int**.

Actually, if you pick the correct types, you don't have a problem with
types and pointers to pointers.
/* BEGIN new.c */

#include <stdio.h>

int main(void)
{
void *pointer = &pointer;

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


ITYM "pointer is %p\n"


No. I was emphasizing the fact that they compare equal.
printf("&pointer is %p\n", (void *)&pointer);
}
return 0;
}

/* END new.c */


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.

--
pete
Nov 15 '05 #21
pete <pf*****@mindspring.com> writes:
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);
}

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #22
Keith Thompson wrote:

pete <pf*****@mindspring.com> writes:
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.

--
pete
Nov 15 '05 #23
In article <43***********@mindspring.com>,
pete <pf*****@mindspring.com> wrote:
(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.)
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
Nov 15 '05 #24
pete <pf*****@mindspring.com> writes:
Keith Thompson wrote:

pete <pf*****@mindspring.com> writes:
> 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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #25
Walter Roberson wrote:
In article <43***********@mindspring.com>,
pete <pf*****@mindspring.com> wrote:
(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.)


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.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #26
>Keith Thompson wrote:
[snippage]
printf("&pointer is %p\n", pointer); [should have read] printf("pointer is %p\n", pointer);

In article <43***********@mindspring.com>
pete <pf*****@mindspring.com> wrote:(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);

:-)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 15 '05 #27
Keith Thompson wrote:

pete <pf*****@mindspring.com> writes:
Keith Thompson wrote:

pete <pf*****@mindspring.com> writes:
> 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.

--
pete
Nov 15 '05 #28
Flash Gordon wrote:

Walter Roberson wrote:
In article <43***********@mindspring.com>,
pete <pf*****@mindspring.com> wrote:
(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.)


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.

--
pete
Nov 15 '05 #29
pete <pf*****@mindspring.com> writes:
Keith Thompson wrote:
pete <pf*****@mindspring.com> writes:
> Keith Thompson wrote:
>>
>> pete <pf*****@mindspring.com> writes:
>> > 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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #30
Chris Torek wrote:
Keith Thompson wrote:

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


In article <43***********@mindspring.com>
pete <pf*****@mindspring.com> wrote:
(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.

--
pete
Nov 15 '05 #31
On 2005-09-29, Walter Roberson <ro******@ibd.nrc-cnrc.gc.ca> wrote:
In article <11**********************@g44g2000cwa.googlegroups .com>,
tedu <tu@zeitbombe.org> wrote:
dough wrote:
Is it possible in C to declare and initialize a pointer that points to
itself? Why or why not?

Sure, but of what use would it be?


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.

Jan 10 '06 #32
On 10 Jan 2006 22:36:43 +1050, tanvir <ta****@fbi.my.domain> wrote:
On 2005-09-29, Walter Roberson <ro******@ibd.nrc-cnrc.gc.ca> wrote:
In article <11**********************@g44g2000cwa.googlegroups .com>,
tedu <tu@zeitbombe.org> wrote:
dough wrote:
Is it possible in C to declare and initialize a pointer that points to
itself? Why or why not?

Sure, but of what use would it be?


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

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
Jan 16 '06 #33
On 2005-09-29, Walter Roberson <ro******@ibd.nrc-cnrc.gc.ca> wrote:
In article <11**********************@g44g2000cwa.googlegroups .com>,
tedu <tu@zeitbombe.org> wrote:
dough wrote:
Is it possible in C to declare and initialize a pointer that points to
itself? Why or why not?

Sure, but of what use would it be?


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?
Jan 16 '06 #34

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Chris Mantoulidis | last post by:
I see some really weird output from this program (compiled with GCC 3.3.2 under Linux). #include <iostream> using namespace std; int main() { char *s; s = "test1"; cout << "s = " << s << "...
3
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
10
by: quantdev2004 | last post by:
Hi all, I have been deling with this kind of code: class Foo { public: void NonConstMethod() {} };
6
by: Irrwahn Grausewitz | last post by:
Hey, y'all. While doing some pointer experiments I encountered a problem. I know that I know the answer already, but trying to remember I just screwed up my mind. I wonder if someone would be...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
28
by: Wonder | last post by:
Hello, I'm confused by the pointer definition such as int *(p); It seems if the parenthesis close p, it defines only 3 integers. The star is just useless. It can be showed by my program: ...
2
by: Giorgos Keramidas | last post by:
On Sun, 05 Oct 2008 18:22:13 +0300, Giorgos Keramidas <keramida@ceid.upatras.grwrote: My apologies. I should have been less hasty to hit `post'. If showtext() is passed a null pointer, it may...
8
by: tfelb | last post by:
Hey group! I have 2 questions. I saw functions with char *dst = (char *)src. In that case if I remember what I've learned I assign (an) (the) address of src to dst. Right? But I can assign...
6
by: Mahendra | last post by:
I have two cases - 1. When I have a pointer A pointing to a heap memory - What happens when I dereference the pointer A using free(A). It deallocated the heap memory the pointer was pointing...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.