Nested indices in single bracket of an array

Viewed 261

I have never seen anything like this and I already read arrays. Array has 2 indices in a bracket, from which one is nested in another bracket:

printf("%d ", arr[chosen[i]]); 

I have no idea what happens, if there are 2 indices inside a bracket.

Whole code here: https://www.geeksforgeeks.org/combinations-with-repetitions/

4 Answers

Indexing directly by using the int array called chosen.

printf("%d ", arr[chosen[i]]); 

Or indexing indirectly by saving the value first, is the same.

int idx = chosen[i];
printf("%d ", arr[idx]); 
printf("%d ", arr[chosen[i]]);

chosen[i] evaluates to the value which is stored at the (i + 1) . element of array chosen.

But I need more data to illustrate the case. So, let´s assume the following initializations:

int i = 2;
int chosen[4] = {1,5,7,9}
int arr[9] = {6,2,7,23,9,16,11,44,2};

Assuming that i is 2, chosen[i] points to the third element of the array chosen and evaluates to the value 7. Another repetition for the sake of teaching: chosen[i] is evaluated to 7, if i is 2. Note that index counting start at 0, so chosen[2] means the third element of array chosen, not the second.

Thereafter, 7 would be used to determine the 8th element of the array arr, a[7]; a[7] = 8th element of arr.

Inside a[7] we have the value of 44. So we end up printing 44 by:

printf("%d ", arr[chosen[i]]);

So,

printf("%d ", arr[chosen[i]]); 

is in our example equivalent to:

printf("%d ", arr[7]);

To demonstrate the relation another time:

chosen[i] = NUMBER;
buffer = chosen[i];
printf("%d ", arr[buffer]);

is equivalent to

buffer = NUMBER;
printf("%d ", arr[buffer]);

which is also equivalent to

count[i] = NUMBER;
printf("%d ", arr[count[i]);

and of course equivalent to:

printf("%d ", arr[NUMBER]);

You simply need to break down the code, one step at a time.

Both chosen and arr are declared as arrays of integers; so, working from the 'inside out', we first evaluate chosen[i] - this will be the (integer) element of the array chosen at the position (index) given by i.

Let's call this value chosen_int.

We can then re-write the arr[chosen[i]] expression as arr[chosen_int] - which will be the (integer) element of the array arr at the index value given by our previous (inner) expression.

It may help to write out, explicitly, what we have just done, replacing the 'confusing' single-line code with this:

int chosen_int = chosen[i];
printf("%d ", arr[chosen_int]);

Feel free to ask for further clarification and/or explanation.

Just to add something to other replies.

I have never seen anything like this...
printf("%d ", arr[chosen[i]]);

This (that you've never seen that) sounds very strange to me, but it can happen.

There is really nothing strange: if you think, there is no ambiguity. A single couple brackets contains an index; that index is expressed again with an array notation.

It could be useful to change the order of a bulky array. Suppose you have an array containing long structs. To sort it, you should swap large objects. But you can sort it swapping its indexes instead of the objects themselves. Or may be you want to be able to maintain in memory different ordered lists, all containing indexes into the same array.

Related