# Arrays : Setting values in arrays

0 like 0 dislike
236 views

Setting values in arrays

 Where can I do online courses From Word's Top Instructors? UDEMY:: Attend All Udemy Courses in Just INR 450[Coupon] Coursera:: Join For FREE

0 like 0 dislike
by Goeduhub's Expert (9.0k points)

Accessing array values is generally done through square brackets:

int val;

int array[10];

/* Setting the value of the fifth element to 5: */

array[4] = 5;

/* The above is equal to: */

*(array + 4) = 5;

/* Reading the value of the fifth element: */

val = array[4];

As a side eﬀect of the operands to the + operator being exchangeable (--> commutative law) the following is equivalent:

*(array + 4) = 5;

*(4 + array) = 5;

so as well the next statements are equivalent:

array[4] = 5;

4[array] = 5;                            /* Weird but valid C ... */

and those two as well:

val = array[4];

val = 4[array];                         /* Weird but valid C ... */

C doesn't perform any boundary checks, accessing contents outside of the declared array is undeﬁned (Accessing memory beyond allocated chunk ):

int val;

int array[10];

array[4] = 5;    /* ok */

val = array[4];  /* ok */

array[19] = 20;  /* undefined behavior */

val = array[15]; /* undefined behavior */