Home > OS >  What are the steps in this process? When does x refer to the index?
What are the steps in this process? When does x refer to the index?

Time:03-19

All that I really know is that the square bracket x refers to the array.

Image

CodePudding user response:

( I consider the other answer, https://stackoverflow.com/a/71535758/7733418 helpful and correct.
I however intentionally offer a different interpretation, a different understanding of "refer to the index" phrasing in OPs question. I do so because I think the different angle helps OP to sort out their confusion. Maybe OPs thinking approached from this alternate angle and caused the the feeling that "index" is not only within []. I use/copy the other answers phrasing and formatting where I do not intend to have a different angle, to help make the contrast. )

Statement 1

int arr[5]; //this creates an array named arr with 5 elements of type int

Here, in the definition of the array variable, the number inside [] is the total size of the array, in contrast to pretty much all other cases, where the number inside [] is used as index to the array entry to be used in the statement.
There is a similar case, the declaration of an array variable. It tells the compiler that such a variable exists, but that the definition is already elsewehre, usually in a different code file. extern int arr[5];, note the "extern" keyword in the otherwise identical syntax. The declaration also has size instead of index.

Statement 2

int x; //this creates a variable of type int which is uninitialized

The variable will be used to contain the changing value of the index into the array.
The value of the index can be used to actually index into the array, i.e. to select an array element for use.
The value of the index can also be used for other things, like calculating other values.

Statement 3

for(x = 0; x < 5; x  ) // variable x is set up to iterate through all indexes
{
    //--v-----------------> here x is used to select from array by index
    arr[x] = x * x;
    //       ^---^--------> here the value of the currently used index is used
    //                      to calculate the new value,
    //                      which is written into the index array entry
}

Statement 4

for ( x=1; x<5; x = x   3) // x iterates through only a few of the possible indexes
    arry[x]--;             // decrement the array entry at current index

Statement 5

for (x=0; x<2; x  ) // x set up to iterate through only the first two possible indexes

    //   ----------------- here x is used as a write index,
    //  v                  to select the entry to be written
    arr[x] = arr[x   2];
    //           ^-------- x is the current write index
    //           ^^^^^---- x   2, the write index with an offset,
    //                     is the read index

This copies from a read index to a write index. Both are determined by the variable iterating through indexes, but the read index has an offset. The effect is a shift of array elements; more precisely a copy of a few values from higher-indexed array etnries into lower-indexed array entries.

CodePudding user response:

Lets see the meaning of each of the statements in your program.

Statement 1

int arr[5]; //this creates an array named arr with 5 elements of type int

The above statement creates an array named arr with 5 elements of type int. Moreover all elements are default inititliazed.

Statement 2

int x; //this creates a variable of type int which is uninitialized

Statement 3

for(x = 0; x < 5; x  )
{
    //--v-----------------> here x is used as an index
    arr[x] = x * x;
    //-------^---^--------> here x is not used as an index
}

As indicated in the above example, in the expression arr[x], x is used as an index. That is, whatever the value of x, the element at the position/index of the array will be fetched.

On the other hand, in the expression x * x, x is not used as an index.

This essentially means that we're assigning x * x to the element at index x. Thus, after this for loop, the array will contain the elements 0, 1, 4, 9, 16.

Statement 4

Similarly, the expression arr[x]-- means that we're using the postfix decrement operator on the elements at xth index.

Statement 5

Here the statement:

//--v----------------->here x is used as an index
arr[x] = arr[x   2];
//-----------vvvvv---->here x 2 is used as an index

means that we are assigning the element at position/index x 2 to the element at position/index x of the array.

  • Related