Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   a pointer that points to itself (http://www.velocityreviews.com/forums/t439580-a-pointer-that-points-to-itself.html)

dough 09-29-2005 12:42 AM

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?


tedu 09-29-2005 12:56 AM

Re: a pointer that points to itself
 

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?


Walter Roberson 09-29-2005 01:22 AM

Re: a pointer that points to itself
 
In article <1127955372.543088.194620@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

Peter Nilsson 09-29-2005 01:48 AM

Re: a pointer that points to itself
 
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


Skarmander 09-29-2005 01:54 AM

Re: a pointer that points to itself
 
Walter Roberson wrote:
> In article <1127955372.543088.194620@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.

tanmoy87544@gmail.com 09-29-2005 05:22 AM

Re: a pointer that points to itself
 
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.


=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?= 09-29-2005 06:10 AM

Re: a pointer that points to itself
 
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**.

Peter Nilsson 09-29-2005 06:28 AM

Re: a pointer that points to itself
 
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


Villy Kruse 09-29-2005 07:01 AM

Re: a pointer that points to itself
 
On 28 Sep 2005 22:22:27 -0700,
tanmoy87544@gmail.com <tanmoy87544@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

Keith Thompson 09-29-2005 07:23 AM

Re: a pointer that points to itself
 
tanmoy87544@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) kst-u@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.


All times are GMT. The time now is 10:22 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.