Accessing array elements How to access the elements of an array


int main()
{
    double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};
    double salary = balance[4]; //0th to 4th 50.Access 0
}

double *p1;

p1 = array;
printf("%f", *p1); // 1000.0
printf("%f", *(balance + 2)); //3.4

-If only array is used, the address at the beginning of the array is returned.

・ Access to elements If you add 1, *, the first element is returned. 2. By specifying the index with [], the element corresponding to the index is returned.

//Both are 1000.Returns 0
*balance
balance[0]

balance + 2 
/* 
The address that points to the beginning of balance is
It shifts back by two indexes. (Become second//3 for balance.(Points to the address of 4)
*/

To access that element

*(balance + 2) // 3.4

Recommended Posts