Thread: pointer to pointer

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    4

    Thumbs up pointer to pointer

    Code:
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
        static int arr[ ]={0,1,2,3,4};
        int *p[]={arr,arr+1,arr+2,arr+3,arr+4};
        int **ptr=p;
        ptr++;
        printf("%d %d %d\n",ptr-p,*ptr-arr,**ptr);*ptr++;
        printf("%d %d %d\n",ptr-p,*ptr-arr,**ptr);*++ptr;
        printf("%d %d %d\n",ptr-p,*ptr-arr,**ptr);++*ptr;
        printf("%d %d %d\n",ptr-p,*ptr-arr,**ptr);
        printf("\n");
        getch();
        return 0;
    }
    output comes as
    1 1 1
    2 2 2
    3 3 3
    3 4 4
    why the final line atarts with 3? why is it not 4?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Gokulan
    why the final line atarts with 3? why is it not 4?
    Inspect the previous statement. Was ptr incremented?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    Quote Originally Posted by Gokulan View Post
    Code:
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
        static int arr[ ]={0,1,2,3,4};
        int *p[]={arr,arr+1,arr+2,arr+3,arr+4};
        int **ptr=p;
        ptr++;
        printf("%d %d %d\n",ptr-p,*ptr-arr,**ptr);*ptr++;
        printf("%d %d %d\n",ptr-p,*ptr-arr,**ptr);*++ptr;
        printf("%d %d %d\n",ptr-p,*ptr-arr,**ptr);++*ptr;
        printf("%d %d %d\n",ptr-p,*ptr-arr,**ptr);
        printf("\n");
        getch();
        return 0;
    }
    output comes as
    1 1 1
    2 2 2
    3 3 3
    3 4 4
    why the final line atarts with 3? why is it not 4?
    I have a slight confusion on the output of first printf

    when we say *prt-arr, *ptr is a pointer to int and hence holds an address. when we do *ptr - arr, we are subtracting an array arr form address of integer. can someone clear this please

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    ptr is a pointer to a pointer to an int. Thus, *ptr is a pointer to an int. arr (the name of the array by itself) also serves as a pointer to an int, so you are subtracting two pointers. This is a common way, for example, to determine your position in an array give the start and a pointer to an element.

    Code:
    static int arr[ ]={0,1,2,3,4};  // set up an array of ints
    int *p[]={arr,arr+1,arr+2,arr+3,arr+4};  // set up an array of int pointers, each pointing to an element in arr
    int **ptr=p;  // set up a pointer to pointer to int, pointing to the first element of p (which in turn points to the first element of arr)
    ptr++;  // increment ptr to the second element in p (which in turn points to the second element of arr, arr+1)
    *ptr-arr;  // ptr points to the second element of p, so *ptr is the second element, or arr+1.  You then do (arr+1)-arr = 1.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    Quote Originally Posted by anduril462 View Post
    Code:
    static int arr[ ]={0,1,2,3,4};  // set up an array of ints
    int *p[]={arr,arr+1,arr+2,arr+3,arr+4};  // set up an array of int pointers, each pointing to an element in arr
    int **ptr=p;  // set up a pointer to pointer to int, pointing to the first element of p (which in turn points to the first element of arr)
    ptr++;  // increment ptr to the second element in p (which in turn points to the second element of arr, arr+1)
    *ptr-arr;  // ptr points to the second element of p, so *ptr is the second element, or arr+1.  You then do (arr+1)-arr = 1.
    Ok I think I got what u mean when u said u can do arithmetic on pointers. But in the line

    Code:
    *ptr-arr;  // ptr points to the second element of p, so *ptr is the second element, or arr+1.  You then do (arr+1)-arr = 1.
    I know ptr points to the second element of p which is itself a pointer to an array of integers, arr. So *ptr is the "address" of second element of arr+1 and **ptr should be arr+1. So *ptr - arr means (correct me if I am wrong)

    (address of arr+1) - arr[0] isn't it?

    I actually ran the program in gdb and when I printed out the values of *ptr, I indeed got an address and not the value of arr[0]. But it how ever says *ptr - arr is 1. thats where I got confused.
    Last edited by livin; 02-24-2012 at 02:26 PM.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by livin View Post
    (address of arr+1) - arr[0] isn't it?

    I actually ran the program in gdb and when I printed out the values of *ptr, I indeed got an address and not the value of arr[0]. But it how ever says *ptr - arr is 1. thats where I got confused.
    No, there are no brackets there. Using just arr is like saying &arr[0]. It's the address of the first element. It's confusing because arr[0] = 0 and arr[1] = 1. Try changing arr to
    Code:
    static int arr[ ]={10,15,20,25,30};
    You will see that the values in arr are not used anywhere in *ptr-arr. Just the address pointed to by ptr and the address of the start of the array. The reason you get 1 when you subtract *ptr-arr is because C adjusts for the size of the thing pointed to when doing pointer arithmetic. If you examine the values of *ptr and arr in gdb, you will probably see that they are 4 bytes (for a 32-bit machine) or 8-bytes (for a 64-bit machine) apart. But when you do the subtraction, you're really asking "how many 4- or 8-byte integers are between these two addresses", thus the 1.

    Hope that's clear.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    Quote Originally Posted by anduril462 View Post
    But when you do the subtraction, you're really asking "how many 4- or 8-byte integers are between these two addresses", thus the 1.
    Wonderful explanation..thanks a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 21
    Last Post: 01-22-2012, 11:35 AM
  2. Replies: 3
    Last Post: 10-30-2009, 04:41 PM
  3. Replies: 9
    Last Post: 06-13-2009, 02:31 AM
  4. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  5. Replies: 4
    Last Post: 08-27-2007, 11:51 PM