two pointers same address

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • destination
    New Member
    • Dec 2009
    • 34

    two pointers same address

    #include <stdio.h>
    int main(void)
    {
    int a=1,*p=&a,*q=p;
    printf("%p %p",p,q);
    return 0;
    }
    I have a doubt in the above code. There are two pointers p and q .when i print the address of p and q it comes out to be same. how is this possible. I know that both are pointing to same location but still they are two different variables then address corresponding to them sud also be different then how come they have same address??
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You are not printing the address of p and q. You are printing the value of p and q. If these were ints you would see the value of the ints.

    To see the addresses of p and q, print &p and &q.

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      Both p and q hold the address of 'a' 'which is not the address of p or q. If p and q are two different pointers their addresses will be different also.

      Comment

      • destination
        New Member
        • Dec 2009
        • 34

        #4
        #include <stdio.h>
        int main(void)

        {
        int a=1,*p=&a,*q=p;
        printf("%p %p",p,q);
        p++;
        printf("\n%p",p );

        getch();
        }
        LOok at this code snippet.
        Why does this gives 1 i thought it will print 4 on my machine as int takes 4 bytes on my machine.

        Also if i use this line
        printf("\n%p",( char*)p-(char*)q);
        it prints 4 .
        Iam unable to makeout what is going on.
        someone look into this problem.

        Comment

        • newb16
          Contributor
          • Jul 2008
          • 687

          #5
          On my mingw setup if prints
          0022FF74 0022FF74
          0022FF78
          , the value in the second line is 4 bytes higher than in the first line, as expected.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            The %p formatter is implementation dependent. On Visual Studio.NET 2008 I get 0012FF60 which is the address of the int.
            This code:

            Code:
            printf("\n%p",(char*)p-(char*)q);
            tells the compiler to subtract two pointers that are char*. The answer is 4 because the difference between the two addresses is 4. You see, that cast is a lie. These are pointers to int. When you incremented p, you incremented by the sizeof(Int), which is 4 bytes. Had you told the truth and subtracted p-q as pointers to int you would have got the1 instead. That is, the addresses are separated by the sizeof(int).

            Comment

            • destination
              New Member
              • Dec 2009
              • 34

              #7
              I made a mistake in asking the question

              #include <stdio.h>
              int main(void)

              {
              int a=1,*p=&a,*q=p;
              printf("%p %p",p,q);
              p++;
              printf("\n%p",p-q);
              }

              Why does this prints 1 but i expected it to print 4.
              If p initially points to 1000 and q also to 1000 after p++ p will ;point to 1004
              and when i take the difference it sud print 4 but why is it printing 1.
              This is my question.

              Comment

              • newb16
                Contributor
                • Jul 2008
                • 687

                #8
                It's pointer arithmetic. When you increment it ( p++ ) it goes forward four bytes, the sizeof(int) on your platforn. The result of subtraction for two pointers is not a number of raw bytes between them, but number of the items to which they point ( that is, one 'int' )

                Comment

                • destination
                  New Member
                  • Dec 2009
                  • 34

                  #9
                  You mean the difference gives the number of elements between two pointers

                  Comment

                  • newb16
                    Contributor
                    • Jul 2008
                    • 687

                    #10
                    Yes, number of elements between them.

                    Comment

                    Working...