Which of the following gives the memory address of the first element i...
The correct answer is option 'D' - array.
Explanation:
The memory address of the first element in an array can be obtained by simply using the name of the array without any index. Let's understand this in detail:
- In an array, elements are stored in contiguous memory locations. Each element in the array has a unique index starting from 0.
- The name of an array itself represents the memory address of the first element in the array.
- When we use the name of the array without any index, it refers to the memory address of the first element.
Let's consider an example to understand this better. Suppose we have an array called "numbers" with 5 elements:
int numbers[5] = {10, 20, 30, 40, 50};
- The memory representation of this array would be something like this:
| Address | Value |
|---------|-------|
| 1000 | 10 |
| 1004 | 20 |
| 1008 | 30 |
| 1012 | 40 |
| 1016 | 50 |
- In this example, the memory address of the first element (10) is 1000.
Now, let's go through the options given in the question:
a) array[0]: This refers to the first element in the array, but it returns the value of the element, not the memory address.
b) array[1]: This refers to the second element in the array, not the first element.
c) array(2): This is not a valid syntax in C++. Parentheses are used for function calls, not for accessing array elements.
d) array: This is the correct syntax to obtain the memory address of the first element in the array.
Therefore, the correct answer is option 'D' - array, as it represents the memory address of the first element in the array.
Which of the following gives the memory address of the first element i...
To get the address of ith index of an array, we use following syntax (arr + i). So as we need address of first index we will use (arr + 0) equivalent to arr.