Thread: C arrays

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    2

    C arrays

    Hi guys , i confused with arrays ...
    see this foll code
    <code>
    int arr [3];
    printf("%u %u",arr,&arr);
    </code>
    i get the same address for arr and &arr .
    Does this mean & operator is defined in such a way or that there is something to do with arrays , if so what is that .

    Thanks guys.

  2. #2
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    What address do you think it should show?

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Yes, this is a quirk of arrays.
    arr is the address of the first element. &arr is the address of the array, the array starts at the first element, so you'll see &arr == arr.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Since arrays as arguments to functions are translated to the address of the first element, then I would expect that the address of an array is the same as the address of the first element, numerically. They are not the same semantically, as the address of the first element is a pointer of the type of the array (int *), whilst the address of the array is a pointer to array int [3]. But the address of the two would be the same.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Note that you print addresses with &#37;p (and typically cast to void*, as well).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 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. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM

Tags for this Thread