GOEDUHUB Online Courses || Last Batch Student's Projects || COVID-19 Projects(AI-ML) || Universities  ||  Placement Preparation  Subscribe our youtube channel
+91-7976731765 Free Online Tutorials ||  MACHINE LEARNING || Python || DBMS || OOPs || DSA || Java || Linux/Unix ||  C Programming
Register For Online Free Certified Workshops 
0 like 0 dislike
34 views
in Tutorial & Interview questions by Goeduhub's Expert (8k points)

Setting values in arrays

1 Answer

0 like 0 dislike
by Goeduhub's Expert (8k points)
 
Best answer
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 effect 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 undefined (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 */
 Placements:   List of companies | Logical Reasoning Questions | Quantitative Aptitude Questions | General English Questions | Technical-MCQ and Interview Questions
 Important Lists: List of NITsList of IITsList of Exams After Graduation | List of Engineering Entrance Examinations (UG/PG)College ReviewsCollege Fest, Events & WorkshopsKnowledge ShareTrainees/Interns After 15-04-2020
Exams & Cutoffs: JEE Main | JEE Advanced | GATE | IES | ISROList of PSUs || Cutoff-GATECutoff_IIT-JEECS-ScopeECE ScopeEE-Scope
 Download Previous Year Papers For:  GATE | IES | RAJASTHAN TECHNICAL UNIVERSITY (RTU-Kota)RPSC Technical Exams | ISRO
 Goeduhub
About Us | Contact Us   Social::   |  | 
...