![]() |
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? |
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? |
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 |
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 |
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. |
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. |
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**. |
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 |
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 |
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:14 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.