Thread: Memory Address of Array Question

  1. #1
    #junkie
    Join Date
    Oct 2004
    Posts
    240

    Memory Address of Array Question

    It is late, and i was bored so i decided to go through a few tutorial quizes on this site. One question i came apon confused me, how do you get a memory address of a specific element in an array.
    5. Which of the following gives the memory address of the first element in array foo, an array with 100 elements?
    A. foo[0];
    B. foo;
    C. &foo;
    D. foo[1];
    It shows B as being correct, i do not understand why it is not C seeing as C has the memory address operator.

    i mean if "foo;" returns the address of the first element of an array, how do you get say the 2nd element in the 3rd dimension's address.

    Or is it trying to say that arrays keep all dimension's and elements in one memory location. Now that i think about it thats prob right lol... i think. Anyway, enlighten me hehe. Thanks
    01110111011000010110110001100100011011110010000001 11000101110101011010010111010000100000011011000110 10010110011001100101001000000111100101101111011101 0100100000011011100111010101100010

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    78
    foo;

    is same as:

    &foo[0];

    address of second array element is then:

    &foo[1];

    You can also use pointer syntax...

    foo;

    same as

    foo+0;

    then second element is

    foo+1;

    access value of second element using pointer syntax:

    *(foo+1);

    or using array syntax:

    foo[1];



    -Rog

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    5. Which of the following gives the memory address of the first element in array foo, an array with 100 elements?
    A. foo[0];
    B. foo;
    C. &foo;
    D. foo[1];
    Just to confuse you, both B and C are correct!

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Just to confuse you, both B and C are correct!
    And yet, only B is really correct. While when used in value context, the name of an array is converted to a pointer to the first element, using the address-of operator on an array results in the address of the array itself, not the address of the first element. IIRC, the C++ standard doesn't require the address of an array to match the address of the first element of the array even though that is by far the most convenient implementation.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. Accessing variables with memory address
    By ITAmember in forum C Programming
    Replies: 54
    Last Post: 06-28-2009, 03:35 AM
  3. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 07:17 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM