Closed Thread
Results 1 to 5 of 5

Thread: Memory address in array

  1. #1
    Maze's Avatar
    Maze is offline Learning Programmer
    Join Date
    Dec 2008
    Posts
    32
    Rep Power
    0

    Memory address in array

    I was solving a quiz and this is a question:

    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];

    Both foo and &foo gives the same result. Which one is true?

    Thanks

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,525
    Blog Entries
    75
    Rep Power
    144

    Re: Memory address in array

    I believe they want "foo"
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Mathematix is offline Programmer
    Join Date
    Jun 2009
    Posts
    112
    Rep Power
    0

    Re: Memory address in array

    Given that this is quick...

    A. foo[0];

    This will dereference the pointer to the first element of the array 'foo'.

    B. foo;

    This will return the address of the start of the array 'foo' and is permitted to be NULL.

    C. &foo;

    This will return the address of the start of the array 'foo' and is not permitted to be NULL. Array 'foo' must either be allocated or statically declared, or pointing to another valid variable of some kind that isn't NULL.

    Google 'references versus pointers in C'.

    D. foo[1];

    This will dereference the second element of the array.

  5. #4
    Maze's Avatar
    Maze is offline Learning Programmer
    Join Date
    Dec 2008
    Posts
    32
    Rep Power
    0

    Re: Memory address in array

    Thanks for answer. Nice to have answer.

    I have a question, you said permited to be NULL; could you make it clear please. Thanks

  6. #5
    Mathematix is offline Programmer
    Join Date
    Jun 2009
    Posts
    112
    Rep Power
    0

    Re: Memory address in array

    Quote Originally Posted by Maze View Post
    Thanks for answer. Nice to have answer.

    I have a question, you said permited to be NULL; could you make it clear please. Thanks
    Well...

    Code:
    int* ptr = NULL;
    is valid and reliable C/C++ code.

    But!

    Code:
    int* ptr = NULL; 
    int& ref = (int &)ptr;
    Might build, but can and most likely will result in undefined behaviour if 'ref' is used in your code.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to find the value in memory address ?
    By SLiM_BoY in forum C and C++
    Replies: 1
    Last Post: 10-02-2011, 12:03 AM
  2. Replies: 4
    Last Post: 05-04-2011, 09:09 AM
  3. Replies: 5
    Last Post: 04-30-2011, 04:12 PM
  4. Replies: 4
    Last Post: 10-06-2008, 07:05 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts