a pointer that points to itself

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dough

    #1

    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

    #2
    Re: a pointer that points to itself


    dough wrote:[color=blue]
    > Is it possible in C to declare and initialize a pointer that points to
    > itself? Why or why not?[/color]

    Sure, but of what use would it be?

    Comment

    • Walter Roberson

      #3
      Re: a pointer that points to itself

      In article <1127955372.543 088.194620@g44g 2000cwa.googleg roups.com>,
      tedu <tu@zeitbombe.o rg> wrote:
      [color=blue]
      >dough wrote:[color=green]
      >> Is it possible in C to declare and initialize a pointer that points to
      >> itself? Why or why not?[/color][/color]
      [color=blue]
      >Sure, but of what use would it be?[/color]

      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

      Comment

      • Peter Nilsson

        #4
        Re: a pointer that points to itself

        Walter Roberson wrote:[color=blue]
        > tedu <tu@zeitbombe.o rg> wrote:[color=green]
        > > dough wrote:[color=darkred]
        > > > Is it possible in C to declare and initialize a pointer that points
        > > > to itself? Why or why not?[/color][/color]
        >[color=green]
        > > Sure, but of what use would it be?[/color]
        >
        > 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.[/color]

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

        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

        Comment

        • Skarmander

          #5
          Re: a pointer that points to itself

          Walter Roberson wrote:[color=blue]
          > In article <1127955372.543 088.194620@g44g 2000cwa.googleg roups.com>,
          > tedu <tu@zeitbombe.o rg> wrote:
          >
          >[color=green]
          >>dough wrote:
          >>[color=darkred]
          >>>Is it possible in C to declare and initialize a pointer that points to
          >>>itself? Why or why not?[/color][/color]
          >
          >[color=green]
          >>Sure, but of what use would it be?[/color]
          >
          >
          > 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.
          >[/color]
          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.

          Comment

          • tanmoy87544@gmail.com

            #6
            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.

            Comment

            • Nils O. Selåsdal

              #7
              Re: a pointer that points to itself

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

              Comment

              • Peter Nilsson

                #8
                Re: a pointer that points to itself

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

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

                --
                Peter

                Comment

                • Villy Kruse

                  #9
                  Re: a pointer that points to itself

                  On 28 Sep 2005 22:22:27 -0700,
                  tanmoy87544@gma il.com <tanmoy87544@gm ail.com> wrote:

                  [color=blue]
                  > 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.
                  >[/color]


                  "After the declarator", is that at the following semicolon or comma,
                  or immediately after the name itself but before a possible equals sign?


                  Villy

                  Comment

                  • Keith Thompson

                    #10
                    Re: a pointer that points to itself

                    tanmoy87544@gma il.com writes:[color=blue]
                    > 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.[/color]

                    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.c om, 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_Keit h) 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.

                    Comment

                    • Flash Gordon

                      #11
                      Re: a pointer that points to itself

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

                      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.

                      Comment

                      • Prawit Chaivong

                        #12
                        Re: a pointer that points to itself


                        dough wrote:[color=blue]
                        > Is it possible in C to declare and initialize a pointer that points to
                        > itself? Why or why not?[/color]


                        char * a = (char*) &a;

                        Comment

                        • pete

                          #13
                          Re: a pointer that points to itself

                          Flash Gordon wrote:[color=blue]
                          >
                          > Nils O. Selåsdal wrote:[color=green]
                          > > tedu wrote:
                          > >[color=darkred]
                          > >> 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?[/color]
                          > >
                          > > actually no. The types would be incompatible.
                          > > You can't do int *foo = &foo; &foo has type int**.[/color]
                          >
                          > Actually, if you pick the correct types, you don't have a problem with
                          > types and pointers to pointers.[/color]

                          /* BEGIN new.c */

                          #include <stdio.h>

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

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

                          /* END new.c */


                          --
                          pete

                          Comment

                          • Skarmander

                            #14
                            Re: a pointer that points to itself

                            tanmoy87544@gma il.com wrote:[color=blue]
                            > Skarmander restored context when he copied:[color=green]
                            >> 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;
                            >>[/color]
                            > 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.
                            >[/color]
                            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.

                            Comment

                            • Lawrence Kirby

                              #15
                              Re: a pointer that points to itself

                              On Wed, 28 Sep 2005 23:28:07 -0700, Peter Nilsson wrote:
                              [color=blue]
                              > Nils O. Selåsdal wrote:[color=green]
                              >> tedu wrote:[color=darkred]
                              >> > 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?[/color]
                              >>
                              >> actually no. The types would be incompatible.
                              >> You can't do int *foo = &foo; &foo has type int**.[/color]
                              >
                              > But you can change int for another type which makes it work.[/color]

                              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

                              Comment

                              Working...