pointer doubts...

This is a discussion on pointer doubts... within the C Programming forums, part of the General Programming Boards category; Hi, I have following code Code: main() { int a[3][4] = { 1,2,3,4, 5,6,7,8, 9,10,11,12 }; printf("\n%u\n%u\n%u\n%u\n%u\n",a[0]+1,*(a[0]+1),*(*(a+0)+1),**(a+1),(&a+1)); ; return 0; ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    139

    pointer doubts...

    Hi,

    I have following code
    Code:
    main()
    {
    	int a[3][4] = { 1,2,3,4,
    		              5,6,7,8,   
    	                     9,10,11,12
    					};
    printf("\n%u\n%u\n%u\n%u\n%u\n",a[0]+1,*(a[0]+1),*(*(a+0)+1),**(a+1),(&a+1));
    ;
    
    	return 0;
    }
    1. Why does **a take me to the first element in the second row? Shouldn't it point to the address of content of a[0] . As per the double pointer..

    2. why does (a+1) takes me to the 2nd row and (&a+1) takes me to the end of array? Isn't a and &a one and the same?

    Thanks in advance....

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    Quote Originally Posted by sanddune008
    1. Why does **a take me to the first element in the second row? Shouldn't it point to the address of content of a[0] . As per the double pointer..
    **a is equivalent to a[0][0], so it should give you the value of the first element of the first array.

    Quote Originally Posted by sanddune008
    2. why does (a+1) takes me to the 2nd row and (&a+1) takes me to the end of array? Isn't a and &a one and the same?
    a is a two dimensional array. It is converted to a pointer to the first array. Hence, (a+1) is a pointer to the second array. But &a is the address of the array itself. Hence, &a+1 is the address of the next two dimensional array (even though such an array does not exist).

    A few things to note:
    • Include the appropriate headers. In this case #include <stdio.h>
    • main should return an int, so declare it as such instead of relying on default int.
    • It is good practice to make full use of braces when initialising a two dimensional array (except possibly when performing zero initialisation, which is not what you are doing here).
    • Pointers should be printed with the %p format specifier. You should cast the corresponding arguments to void*.
    • Your indentation could be improved.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1 - You don't actually have **a anywhere in your code. You have **(a+1).
    2 - a is not a pointer. You can't act like it is all the time. It doesn't work exactly the same. Start here: Arrays and Pointers


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    139
    Quote Originally Posted by laserlight View Post
    **a is equivalent to a[0][0], so it should give you the value of the first element of the first array.
    [/list]
    but the same double pointer behaves differently in code below
    Code:
    int **p,*a =8;
    p=&a;
    My question is why does the double indirection behaves differently with an array declaration? How does the compiler deduce this?
    Last edited by sanddune008; 07-08-2009 at 02:44 AM.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    139
    why?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You are abusing pointers terribly, and should really start compiling with warnings on. Why are you assigning a pointer the value of 8? You really should read the link in my previous post, and all associated links on that page.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    139
    Thanks for your time..............

    Quote Originally Posted by quzah View Post
    You are abusing pointers terribly, and should really start compiling with warnings on. Why are you assigning a pointer the value of 8? You really should read the link in my previous post, and all associated links on that page.
    Quzah.
    I am not abusing....I am trying to understand the behaviour of double indirection(**).

    Code:
    p= &a;
    *p; // This will give me address of 8
    **p; // This will give me value 8
    But why is not the same with **a (array pls see the first post)

    besides this let go through the link u mentioned earlier..

  8. #8
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    You can't assign a constant to a pointer. It's decide by the compiler which address to assign the variable and the pointer can point to the variable's address. So, in effect this is wrong
    int *a=8;
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> But why is not the same with **a (array pls see the first post)

    Because a multidimensional array of int's is not the same as an array of pointers to ints. In other words, 'a' is just the starting address of 3 * 4 * sizeof( int ) contigious blocks of data.

    Let's say a[ 0 ][ 0 ] is set to 1024. Interpreted as an int*, *a is this first block (1024). Fine. But interpreting it as an int**, **a would load the contents of *a (1024) and reinterpret it as a pointer! Obviously not what you want.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Perhaps I also got doubtful. Is assigning a constant to a pointer legal in C? What actually happens when we do that ?
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    Giving a pointer a hard coded address is like forwarding your mail before you move out. You're saying you know exactly where something should be stored. It's legal, just boneheaded all the way around.

  12. #12
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Is assigning a constant to a pointer legal in C?

    It's legal, but usually not a very good idea.

    >> What actually happens when we do that ?

    It just sets it to that particular address. Back in the old days, that was sometimes useful, as you could, say, access video memory directly via a known address (which didn't always work, of course, depending on the actual configuration and capabilities of the computer). Nowdays, this sort of thing is quite rare, and in fact the only hard-coded address you generally see is 0!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 04:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 08:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21