printf("%d",*(&ptr2 + 2))

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

    printf("%d",*(&ptr2 + 2))

    Dear all,

    why in the following program

    #include<stdio. h>
    #include<stdlib .h>

    int main(void)
    {

    int x[] = {99,2,3,4,5};
    int *ptr,**ptr2;

    ptr = x;
    ptr2 = &ptr;

    printf("%d",*(& ptr2 + 2));

    return EXIT_SUCCESS;
    }

    printf("%d",*(& ptr2 + 2)); is printing the first element of the array
    x ? can anybody explain ?
  • Ian Collins

    #2
    Re: printf(&quot;%d &quot;,*(&amp;p tr2 + 2))

    sophia wrote:
    Dear all,
    >
    why in the following program
    >
    #include<stdio. h>
    #include<stdlib .h>
    >
    int main(void)
    {
    >
    int x[] = {99,2,3,4,5};
    int *ptr,**ptr2;
    >
    ptr = x;
    ptr2 = &ptr;
    >
    printf("%d",*(& ptr2 + 2));
    >
    return EXIT_SUCCESS;
    }
    >
    printf("%d",*(& ptr2 + 2)); is printing the first element of the array
    x ? can anybody explain ?
    Where do people find these contrived examples?

    The answer is luck. I just happens that's the way the variables are
    laid out in memory.

    Try building 64 bit and see what you get then.

    --
    Ian Collins.

    Comment

    • sophia

      #3
      Re: printf(&quot;%d &quot;,*(&amp;p tr2 + 2))

      On Apr 25, 12:56 pm, Ian Collins <ian-n...@hotmail.co mwrote:
      sophia wrote:
      Dear all,
      >
      why in the following program
      >
       #include<stdio. h>
       #include<stdlib .h>
      >
       int main(void)
       {
      >
         int x[] = {99,2,3,4,5};
         int *ptr,**ptr2;
      >
         ptr  = x;
         ptr2 = &ptr;
      >
         printf("%d",*(& ptr2 + 2));
      >
         return EXIT_SUCCESS;
       }
      >
      printf("%d",*(& ptr2 + 2)); is printing the first element of the array
      x ? can anybody explain ?
      >
      Where do people find these contrived examples?
      >
      The answer is luck.  I just happens that's the way the variables are laid out in memory.
      There is something more than luck, that is my conclusion.

      I tried changing the value of x[0] to different values but still
      it will print the o/p correctly.

      Has it any thing to do with the compiler ?

      I checked this program on lcc-win32 compiler
      Try building 64 bit and see what you get then.
      >
      I don't have a 64 bit compiler

      Comment

      • santosh

        #4
        Re: printf(&quot;%d &quot;,*(&amp;p tr2 + 2))

        sophia wrote:
        Dear all,
        >
        why in the following program
        >
        #include<stdio. h>
        #include<stdlib .h>
        >
        int main(void)
        {
        >
        int x[] = {99,2,3,4,5};
        int *ptr,**ptr2;
        >
        ptr = x;
        ptr2 = &ptr;
        >
        printf("%d",*(& ptr2 + 2));
        >
        return EXIT_SUCCESS;
        }
        >
        printf("%d",*(& ptr2 + 2)); is printing the first element of the array
        x ? can anybody explain ?
        What it prints is purely by chance.
        Did your compiler not produce a warning?
        What is the type of &ptr2? It's int ***. What is the type of (&ptr2 +
        2). Again the same. Then what's the type of *(&ptr2 + 2)? It's int **.
        What type does %d expect. It expects an int.

        What you want is:

        printf("%d\n", **ptr2);

        Comment

        • santosh

          #5
          Re: printf(&quot;%d &quot;,*(&amp;p tr2 + 2))

          sophia wrote:
          On Apr 25, 12:56 pm, Ian Collins <ian-n...@hotmail.co mwrote:
          >sophia wrote:
          Dear all,
          >>
          why in the following program
          >>
          #include<stdio. h>
          #include<stdlib .h>
          >>
          int main(void)
          {
          >>
          int x[] = {99,2,3,4,5};
          int *ptr,**ptr2;
          >>
          ptr  = x;
          ptr2 = &ptr;
          >>
          printf("%d",*(& ptr2 + 2));
          >>
          return EXIT_SUCCESS;
          }
          >>
          printf("%d",*(& ptr2 + 2)); is printing the first element of the
          array x ? can anybody explain ?
          >>
          >Where do people find these contrived examples?
          >>
          >The answer is luck.  I just happens that's the way the variables are
          >laid out in memory.
          >
          There is something more than luck, that is my conclusion.
          Your conclusion is wrong. What it prints is purely by chance, since
          there is a type discrepancy between what printf expects and what it
          gets.
          I tried changing the value of x[0] to different values but still
          it will print the o/p correctly.
          That's irrelevant.
          Has it any thing to do with the compiler ?
          Certainly your output, because your program invokes undefined behaviour,
          is likely to be specific to your implementation or even specific to
          each run of the program.
          I checked this program on lcc-win32 compiler
          >
          >Try building 64 bit and see what you get then.
          >>
          I don't have a 64 bit compiler

          Comment

          • Ian Collins

            #6
            Re: printf(&quot;%d &quot;,*(&amp;p tr2 + 2))

            sophia wrote:
            On Apr 25, 12:56 pm, Ian Collins <ian-n...@hotmail.co mwrote:
            >sophia wrote:
            >>Dear all,
            >>why in the following program
            >> #include<stdio. h>
            >> #include<stdlib .h>
            >> int main(void)
            >> {
            >> int x[] = {99,2,3,4,5};
            >> int *ptr,**ptr2;
            >> ptr = x;
            >> ptr2 = &ptr;
            >> printf("%d",*(& ptr2 + 2));
            >> return EXIT_SUCCESS;
            >> }
            >>printf("%d",* (&ptr2 + 2)); is printing the first element of the array
            >>x ? can anybody explain ?
            >Where do people find these contrived examples?
            >>
            >The answer is luck. I just happens that's the way the variables are laid out in memory.
            >
            There is something more than luck, that is my conclusion.
            >
            I tried changing the value of x[0] to different values but still
            it will print the o/p correctly.
            >
            The luck is in the memory layout, not the values. The address of &ptr2
            + 2 just happens to be the address of x[0].
            Has it any thing to do with the compiler ?
            >
            Everything, memory layout is compiler and platform specific, although
            most 32 bit compilers on x86 probably use the same layout.
            I don't have a 64 bit compiler
            Every home should have one :)

            --
            Ian Collins.

            Comment

            • Richard Heathfield

              #7
              Re: printf(&quot;%d &quot;,*(&amp;p tr2 + 2))

              sophia said:
              Dear all,
              >
              why in the following program
              >
              #include<stdio. h>
              #include<stdlib .h>
              >
              int main(void)
              {
              >
              int x[] = {99,2,3,4,5};
              int *ptr,**ptr2;
              >
              ptr = x;
              ptr2 = &ptr;
              >
              printf("%d",*(& ptr2 + 2));
              >
              return EXIT_SUCCESS;
              }
              >
              printf("%d",*(& ptr2 + 2)); is printing the first element of the array
              x ? can anybody explain ?
              The program is broken. In any case, it's overly complex for its job.

              First, decide what you want to print. Then try to think of the simplest
              reasonable way to print it. If you get stuck with that, let us know.

              For example, it would appear from the above that you wish to print the
              number 3. The simplest way to do this is as follows:

              #include <stdio.h>

              int main(void)
              {
              puts("3");
              return 0;
              }

              --
              Richard Heathfield <http://www.cpax.org.uk >
              Email: -http://www. +rjh@
              Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
              "Usenet is a strange place" - dmr 29 July 1999

              Comment

              • sophia

                #8
                Re: printf(&quot;%d &quot;,*(&amp;p tr2 + 2))

                On Apr 25, 2:31 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
                sophia said:
                >
                >
                >
                >
                >
                Dear all,
                >
                why in the following program
                >
                 #include<stdio. h>
                 #include<stdlib .h>
                >
                 int main(void)
                 {
                >
                   int x[] = {99,2,3,4,5};
                   int *ptr,**ptr2;
                >
                   ptr  = x;
                   ptr2 = &ptr;
                >
                   printf("%d",*(& ptr2 + 2));
                >
                   return EXIT_SUCCESS;
                 }
                >
                printf("%d",*(& ptr2 + 2)); is printing the first element of the array
                x ? can anybody explain ?
                >
                The program is broken. In any case, it's overly complex for its job.
                >
                First, decide what you want to print. Then try to think of the simplest
                reasonable way to print it. If you get stuck with that, let us know.
                >
                For example, it would appear from the above that you wish to print the
                number 3. The simplest way to do this is as follows:
                >
                #include <stdio.h>
                >
                int main(void)
                {
                  puts("3");
                  return 0;
                >
                }
                you see, i am not playing games by printing numbers. this is a
                question which was asked in an interview

                Comment

                • Willem

                  #9
                  Re: printf(&quot;%d &quot;,*(&amp;p tr2 + 2))

                  sophia wrote:
                  <snip>
                  )    int x[] = {99,2,3,4,5};
                  )    int *ptr,**ptr2;
                  )>
                  )    ptr  = x;
                  )    ptr2 = &ptr;
                  )>
                  )    printf("%d",*(& ptr2 + 2));
                  <snip>
                  )>
                  ) printf("%d",*(& ptr2 + 2)); is printing the first element of the array
                  ) x ? can anybody explain ?
                  <snip>
                  )
                  ) you see, i am not playing games by printing numbers. this is a
                  ) question which was asked in an interview

                  Then it's not a very good interview, unless they're looking for very
                  specific in-depth knowledge on how a specific compiler works.


                  SaSW, Willem
                  --
                  Disclaimer: I am in no way responsible for any of the statements
                  made in the above text. For all I know I might be
                  drugged or something..
                  No I'm not paranoid. You all think I'm paranoid, don't you !
                  #EOT

                  Comment

                  • Richard Heathfield

                    #10
                    Re: printf(&quot;%d &quot;,*(&amp;p tr2 + 2))

                    sophia said:
                    On Apr 25, 2:31 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
                    >sophia said:
                    >>
                    >>
                    >>
                    >>
                    >>
                    Dear all,
                    >>
                    why in the following program
                    >>
                    #include<stdio. h>
                    #include<stdlib .h>
                    >>
                    int main(void)
                    {
                    >>
                    int x[] = {99,2,3,4,5};
                    int *ptr,**ptr2;
                    >>
                    ptr = x;
                    ptr2 = &ptr;
                    >>
                    printf("%d",*(& ptr2 + 2));
                    >>
                    return EXIT_SUCCESS;
                    }
                    >>
                    printf("%d",*(& ptr2 + 2)); is printing the first element of the array
                    x ? can anybody explain ?
                    >>
                    >The program is broken. In any case, it's overly complex for its job.
                    >>
                    <snip>
                    you see, i am not playing games by printing numbers. this is a
                    question which was asked in an interview
                    Fine, then the answer is easy. "The program is broken, because the
                    expression (&ptr2 + 2) forms an illegal pointer value." That will get you
                    full marks, provided that the interviewer knows C. If the interviewer only
                    /thinks/ he knows C, either become a mind-reader very quickly or look
                    elsewhere for a job.

                    --
                    Richard Heathfield <http://www.cpax.org.uk >
                    Email: -http://www. +rjh@
                    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                    "Usenet is a strange place" - dmr 29 July 1999

                    Comment

                    • santosh

                      #11
                      Re: printf(&quot;%d &quot;,*(&amp;p tr2 + 2))

                      sophia wrote:
                      On Apr 25, 2:31 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
                      >sophia said:
                      >>
                      >>
                      >>
                      >>
                      >>
                      Dear all,
                      >>
                      why in the following program
                      >>
                      #include<stdio. h>
                      #include<stdlib .h>
                      >>
                      int main(void)
                      {
                      >>
                      int x[] = {99,2,3,4,5};
                      int *ptr,**ptr2;
                      >>
                      ptr  = x;
                      ptr2 = &ptr;
                      >>
                      printf("%d",*(& ptr2 + 2));
                      >>
                      return EXIT_SUCCESS;
                      }
                      >>
                      printf("%d",*(& ptr2 + 2)); is printing the first element of the
                      array x ? can anybody explain ?
                      >>
                      >The program is broken. In any case, it's overly complex for its job.
                      >>
                      >First, decide what you want to print. Then try to think of the
                      >simplest reasonable way to print it. If you get stuck with that, let
                      >us know.
                      >>
                      >For example, it would appear from the above that you wish to print
                      >the number 3. The simplest way to do this is as follows:
                      >>
                      >#include <stdio.h>
                      >>
                      >int main(void)
                      >{
                      >puts("3");
                      >return 0;
                      >>
                      >}
                      >
                      you see, i am not playing games by printing numbers. this is a
                      question which was asked in an interview
                      Unless the interviewer provided specific additional details regarding
                      the memory layout of the implementation, the question is broken and the
                      program is undefined according to the language standard. You might try
                      seeking a job elsewhere. Even if it were to be assumed that the
                      array 'x' starts 2 * sizeof &ptr2 bytes after &ptr2, the program is
                      still broken because the printf call is incorrect.

                      Comment

                      • santosh

                        #12
                        Re: printf(&quot;%d &quot;,*(&amp;p tr2 + 2))

                        Richard Heathfield wrote:
                        sophia said:
                        >
                        >Dear all,
                        >>
                        >why in the following program
                        >>
                        > #include<stdio. h>
                        > #include<stdlib .h>
                        >>
                        > int main(void)
                        > {
                        >>
                        > int x[] = {99,2,3,4,5};
                        > int *ptr,**ptr2;
                        >>
                        > ptr = x;
                        > ptr2 = &ptr;
                        >>
                        > printf("%d",*(& ptr2 + 2));
                        >>
                        > return EXIT_SUCCESS;
                        > }
                        >>
                        >printf("%d",*( &ptr2 + 2)); is printing the first element of the array
                        >x ? can anybody explain ?
                        >
                        The program is broken. In any case, it's overly complex for its job.
                        >
                        First, decide what you want to print. Then try to think of the
                        simplest reasonable way to print it. If you get stuck with that, let
                        us know.
                        >
                        For example, it would appear from the above that you wish to print the
                        number 3. The simplest way to do this is as follows:
                        >
                        #include <stdio.h>
                        >
                        int main(void)
                        {
                        puts("3");
                        return 0;
                        }
                        >
                        Even simpler and correct would be:

                        /* begin code */
                        #include <stdio.h>

                        int main(void)
                        {
                        putc('3', whatever_stream );
                        return 0;
                        }
                        /* end code */

                        :-)

                        Comment

                        • Richard Tobin

                          #13
                          Re: printf(&quot;%d &quot;,*(&amp;p tr2 + 2))

                          In article <slrng13712.1lh 1.willem@snail. stack.nl>,
                          Willem <willem@stack.n lwrote:
                          >) you see, i am not playing games by printing numbers. this is a
                          >) question which was asked in an interview
                          >Then it's not a very good interview, unless they're looking for very
                          >specific in-depth knowledge on how a specific compiler works.
                          Or they're looking for someone who knows that it depends on the
                          compiler, and who is able to explain why it's quite likely to produce
                          a certain result on typical implementations .

                          -- Richard
                          --
                          :wq

                          Comment

                          • Barry Schwarz

                            #14
                            Re: printf(&quot;%d &quot;,*(&amp;p tr2 + 2))

                            On Fri, 25 Apr 2008 00:51:33 -0700 (PDT), sophia
                            <sophia.agnes@g mail.comwrote:
                            >Dear all,
                            >
                            >why in the following program
                            >
                            #include<stdio. h>
                            #include<stdlib .h>
                            >
                            int main(void)
                            {
                            >
                            int x[] = {99,2,3,4,5};
                            int *ptr,**ptr2;
                            >
                            ptr = x;
                            ptr2 = &ptr;
                            >
                            printf("%d",*(& ptr2 + 2));
                            >
                            return EXIT_SUCCESS;
                            }
                            >
                            >printf("%d",*( &ptr2 + 2)); is printing the first element of the array
                            >x ? can anybody explain ?
                            As it stands, your code invokes undefined behavior because the second
                            argument is not the int that the %d promises printf it would be.

                            If you change the & to an *, I think it will do what you want.


                            Remove del for email

                            Comment

                            • fred.l.kleinschmidt@boeing.com

                              #15
                              Re: printf(&quot;%d &quot;,*(&amp;p tr2 + 2))

                              On Apr 25, 1:38 am, sophia <sophia.ag...@g mail.comwrote:
                              On Apr 25, 2:31 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
                              >
                              >
                              >
                              >
                              >
                              sophia said:
                              >
                              Dear all,
                              >
                              why in the following program
                              >
                               #include<stdio. h>
                               #include<stdlib .h>
                              >
                               int main(void)
                               {
                              >
                                 int x[] = {99,2,3,4,5};
                                 int *ptr,**ptr2;
                              >
                                 ptr  = x;
                                 ptr2 = &ptr;
                              >
                                 printf("%d",*(& ptr2 + 2));
                              >
                                 return EXIT_SUCCESS;
                               }
                              >
                              printf("%d",*(& ptr2 + 2)); is printing the first element of the array
                              x ? can anybody explain ?
                              >
                              The program is broken. In any case, it's overly complex for its job.
                              >
                              First, decide what you want to print. Then try to think of the simplest
                              reasonable way to print it. If you get stuck with that, let us know.
                              >
                              For example, it would appear from the above that you wish to print the
                              number 3. The simplest way to do this is as follows:
                              >
                              #include <stdio.h>
                              >
                              int main(void)
                              {
                                puts("3");
                                return 0;
                              >
                              }
                              >
                              you see, i am not playing games by printing numbers. this is a
                              question which was asked in an interview- Hide quoted text -
                              >
                              - Show quoted text -
                              What do you think would happen if you changed
                              int *ptr, **ptr2;
                              to
                              int **ptr2, *ptr;
                              ?
                              Try answering it before you program it.
                              Do you know *why* that is the result?
                              --
                              Fred Kleinschmidt

                              Comment

                              Working...