Summer training at Goeduhub Technologies-Jaipur
Register for Free Certified Workshop on RTU Python Lab (VI SEM):: 01-03-2020 ||  Career options for aspiring CS/ITECEEE or EIC or EEE Engineers
0 like 0 dislike
21 views
in Tutorial & Interview questions by Summer Trainee (6.9k points)

Setting values in arrays

1 Answer

0 like 0 dislike
by Summer Trainee (6.9k 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 */

Related questions

0 like 0 dislike
1 answer
 Go to your Branch CSE or IT | ECE | EE, EIC or EEEMECE
 Know About Popular Colleges/Universities  List of IITsList of NITs | RTU-KOTA | Manipal University-Jaipur | JECRC University | Amity University Jaipur | BIT Mesra-Jaipur | MODY UNIVERSITY | LNMIIT-Jaipur | JK Lakshmipat | Banasthali Vidyapith | POORNIMA University
 Exams:   List of Exams After Graduation | List of Engineering Entrance Examinations (UG/PG) | JEE Main | JEE Advanced | GATE | IES | ISROList of PSUs
Placements:  List of companies | Logical Reasoning Questions | Quantitative Aptitude Questions | General English Questions | Technical-MCQ and Interview Questions
 Download Previous Year Papers For:  GATE | IES | RAJASTHAN TECHNICAL UNIVERSITY (RTU-Kota)RPSC Technical Exams | ISRO
 Online Free Training:  Artificial Intelligence(AI) & Machine Learning(ML)Python Programming | Internet of Things-IoT | OpenCV (Open Source Computer Vision Library) | LINUX | Big Data : Hadoop | 
 Goeduhub
About Us | Contact Us   Social::   |  | 
...