address '&' arrays

This is a discussion on address '&' arrays within the C Programming forums, part of the General Programming Boards category; hello everyone somebody plz help me out with this problem. Code: #include<stdio.h> int main() { int arr[]={12,14,78,90}; printf("%u %u \n%u ...

  1. #1
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804

    address '&' arrays

    hello everyone

    somebody plz help me out with this problem.

    Code:
    #include<stdio.h>
    int main()
    {
    int arr[]={12,14,78,90};
    printf("%u %u \n%u %u",arr,&arr,arr+1,&arr+1);
    return 0;
    }
    when i run the above code,why does arr and &arr come out to be same whereas arr+1 and &arr+1 differ.i know that arr refers to the base address but what does &arr means.
    waiting for reply.thanks in advance.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    In this case, 'arr' and '&arr' mean the same thing. However, + has higher precendence than &. Thus arr + 1 is (arr + 1) and &arr + 1 is actually &(arr + 1).

    Have a look at the table: http://www.difranco.net/cop2220/op-prec.htm
    Last edited by zacs7; 07-13-2008 at 07:30 AM.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    &arr is the address of the array. arr is viewed as the address of the first element in the array.

    There isn't a difference at the hardware level in terms of the addresses, but in C, in terms of types, there is a significant difference in cases like this, as you discovered.

    I altered your program to make my compiler happy about those assignments and got these values:

    Code:
    0022FF60 0022FF60
    0022FF64 0022FF70
    When you add a value to a pointer, C does some special additions for you to make it all work out in memory.

    For example, the array, and the array's first element, is located at address 0022FF60 when I ran this. The second element is located at 0022FF64. This is because adding 1, really adds 4, because on my current system an int is 4 bytes. This is automatically done by the compiler, because it takes the size of what arr points to.

    Thus (arr + 1) is really interpreted like this:

    Code:
    arr + (1 * sizeof(*arr))
    On the other hand, ((&arr) + 1) results in 022FF70. Why? Because of the same concept. Remember, &arr is really a pointer to the array, not to the first element. In C, this is very important.

    ((&arr) + 1) is really interpreted like this:

    Code:
    &arr + (1 * sizeof(*(&arr)))
    The size of what arr points to is going to be equal to the size of the array. sizeof(arr) and sizeof(*&arr) both result in 16 on my system, hence the 16-byte jump from 0022FF60 to 0022FF70.

    Thus you see, to C, arr and &arr are NOT the same thing at all times. To the hardware, it's just an address to the same location, but once you start letting C make decisions based upon the type, you have to play by C rules.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 12:16 PM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Help with arrays and pointers please...
    By crazyeyesz28 in forum C++ Programming
    Replies: 8
    Last Post: 03-17-2005, 01:48 PM
  5. HelpMePlease
    By Prncess100 in forum C++ Programming
    Replies: 6
    Last Post: 12-11-2002, 02:02 PM

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