Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > a pointer that points to itself

Reply
Thread Tools

a pointer that points to itself

 
 
pete
Guest
Posts: n/a
 
      09-30-2005
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
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      09-30-2005
pete <(E-Mail Removed)> 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) http://www.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      10-01-2005
Keith Thompson wrote:
>
> pete <(E-Mail Removed)> 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
 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      10-01-2005
In article <(E-Mail Removed)>,
pete <(E-Mail Removed)> 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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-01-2005
pete <(E-Mail Removed)> writes:
> Keith Thompson wrote:
>>
>> pete <(E-Mail Removed)> 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) (E-Mail Removed) <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.
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      10-01-2005
Walter Roberson wrote:
> In article <(E-Mail Removed)>,
> pete <(E-Mail Removed)> 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.
 
Reply With Quote
 
Chris Torek
Guest
Posts: n/a
 
      10-01-2005
>Keith Thompson wrote:
[snippage]
>> printf("&pointer is %p\n", pointer);

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


In article <(E-Mail Removed)>
pete <(E-Mail Removed)> 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.
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      10-02-2005
Keith Thompson wrote:
>
> pete <(E-Mail Removed)> writes:
> > Keith Thompson wrote:
> >>
> >> pete <(E-Mail Removed)> 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
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      10-02-2005
Flash Gordon wrote:
>
> Walter Roberson wrote:
> > In article <(E-Mail Removed)>,
> > pete <(E-Mail Removed)> 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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-02-2005
pete <(E-Mail Removed)> writes:
> Keith Thompson wrote:
>> pete <(E-Mail Removed)> writes:
>> > Keith Thompson wrote:
>> >>
>> >> pete <(E-Mail Removed)> 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) (E-Mail Removed) <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
obtain element name, or attribute and value of the document name itself, and some elemnts and attributes from an ancestor or the node itself using xquery Jeff Kish XML 4 10-30-2008 05:47 PM
evaluate NULL to a pointer which a CONST pointer points to G C++ 3 01-08-2007 04:22 PM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments