+ Reply to Thread
Results 1 to 5 of 5

Thread: Memory address in array

  1. #1
    Learning Programmer Maze is an unknown quantity at this point Maze's Avatar
    Join Date
    Dec 2008
    Posts
    32

    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. #2
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,702
    Blog Entries
    57

    Re: Memory address in array

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

  3. #3
    Programmer Mathematix is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    104

    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.

  4. #4
    Learning Programmer Maze is an unknown quantity at this point Maze's Avatar
    Join Date
    Dec 2008
    Posts
    32

    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

  5. #5
    Programmer Mathematix is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    104

    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.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. PHP Arrays
    By chili5 in forum PHP Tutorials
    Replies: 7
    Last Post: 05-22-2009, 04:16 PM
  2. Memory and Pointers
    By WingedPanther in forum C Tutorials
    Replies: 5
    Last Post: 01-09-2009, 03:02 PM
  3. Replies: 4
    Last Post: 10-06-2008, 09:05 AM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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