Some other valid operations with arrays

Hello,
i cannot figure out what this following line does.
 
  foo[foo[a]] = foo[2] + 5;

Depends what foo is. Depends what operator= does with whatever types are in play here.

What's foo?
What is the a? What type elements does the foo have?

Here is an another code:
1
2
int index  = foo[a];
foo[index] = foo[2] + 5;

Can you figure that out? How does it differ from your example?
it is from this site: http://www.cplusplus.com/doc/tutorial/arrays/:
"
Some other valid operations with arrays:
foo[0] = a;
foo[a] = 75;
b = foo [a+2];
foo[foo[a]] = foo[2] + 5;
"
1
2
3
4
foo[0] = a;  
foo[a] = 75;
b = foo [a+2];
foo[foo[a]] = foo[2] + 5;

 
foo[a]
is 75. So

foo[foo[a]] = foo[2] + 5; is
foo[75] = foo[2] + 5;

Set the value of the 76th element of the array to the value of the third element, plus five.

I think the tutorial's examples try to emphasize the fact that an element of an array can be used like any other variable.


Lets add couple notes about:
1
2
int bar[5];         // declaration of a new array
bar[2] = 75;        // access to an element of the array. 

1. In the declaration the integer value (5) is the number of elements that the array has.

2. The number of elements has to be a compile-time constant for local arrays.

3. In the access the integer value (2) is the index of one element. It must be less than the number of elements.
Topic archived. No new replies allowed.