Thread: address '&' arrays

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

    address '&' arrays

    hello everyone

    somebody plz help me out with this problem.

    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
    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:
    Last edited by zacs7; 07-13-2008 at 06:30 AM.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    &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:

    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:

    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:

    &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, 11:16 AM
  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