Thread: Difference between arr and &arr where arr is 1D array

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    50

    Difference between arr and &arr where arr is 1D array

    Hi

    I have following program

    Code:
    main()
    {
    int arr[]={1,2,3};
    printf("%u %u",arr,&arr);
    printf("%u %u",arr+1,&arr+1);
    
    }
    While printing arr and &arr are giving same address but arr+1 and &arr+1 aregiving different address. What is the difference between arr and &arr

    Thanks

    Sas

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    arr has the address to the base of an array of three int's.

    &arr is the address of the first element of that same array.

    You've arrived at the same address, by going two slightly different ways to get there.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    50
    Why arr+1 and &arr+1 is different?

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well, I think that you should start your understanding of this by answering the following questions:

    1) what is the type of arr
    2) what is the type of &arr

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by SasDutta View Post
    Why arr+1 and &arr+1 is different?
    You're being difficult today, aren't you?

    arr+1 is just the address of the base of the array, plus the address of an int, added on, (since the array knows that it's an int array).

    Caught you before you could ask, didn't I?

    The answer I get with &arr + 1 is the first address after the array. Which leads me to believe (and I don't know for sure), that K&R set it up to conveniently handle arrayname + number to be the address of the base, and to exclude any info on the size of the array, just for the sake of convenience for the programmers. &arrayname + number, was never set up for that, and converts to the first address for the type, beyond the end of the array.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Adak View Post
    arr has the address to the base of an array of three int's.

    &arr is the address of the first element of that same array.
    It's actually the other way around.

    arr is an array. It is also (equivalent to) a pointer an int, which contains the address of the first element of the array.

    &arr is the address of an array of three ints. It is of type pointer to an array of three ints.

    Quote Originally Posted by Adak View Post
    You've arrived at the same address, by going two slightly different ways to get there.
    They may be the same address, but arr and &arr are of different types, which is the reason arr+1 and &arr+1 have different values.

    Pointer arithmetic always relies on the size of the type pointed to.
    Last edited by grumpy; 04-13-2010 at 02:08 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Good discussion, Grumpy. That's what Claudiu was succinctly saying, also. I felt the breeze as his point << flew >> right by me.

  8. #8
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    Now I'm relieved, since I wondered why I though it was the other way around from Adak's answer :-)

Popular pages Recent additions subscribe to a feed