MidTerm Exam Question

Mar 14, 2016 at 1:36am
So I have a question on my midterm that is stumping me; Use the pointer fptr and a for loop to loop through the array of numbers.

I tried to do:
1
2
3
4
5
6
double numbers[10] = { 1,2,3,4,5,6,7,8,9,10 };
double * fptr;
for (fptr = &numbers; fptr < &numbers + 10; fptr++)
{
    std::cout << *fptr
}


Which in my head should assign fptr the memory address of the first element of numbers, then add one to the memory address each pass until it is through the list, but I can't even test because I get the following errors:

 
A value of type double(*)[10] can not be assigned to an entity of type double*


Any ideas? This is my last question and the exam is due at midnight!
Mar 14, 2016 at 1:43am
Hi,

An ordinary array name decays to a pointer.

Doe this help?
Mar 14, 2016 at 2:40am
Not in the slightest.
Mar 14, 2016 at 4:25am
On line 3, you take the address of the array, but the array name is already a pointer. So change &numbers to numbers
Mar 14, 2016 at 7:25am
If you would like to use the address-of operator (&), you can just do
 
fptr = &numbers[0]

This will retrieve the address of the first element in the array. It is exactly the same as
 
fptr = numbers

as numbers points to the first element in the array.

Hope this helps.
Topic archived. No new replies allowed.