Sunday, 19 January 2014

Arrays in C

Not all of us are aware of the fact that C can assign array elements in ANY ORDER!
To specify an array index and element, the proper syntax is as follows:

[index] = value inside curly braces. 

For example :
int arr[] = {[4] = 1, [1] = 5, [2] = 9 };


is equivalent to 
int arr[] = {0,5,9,0,1,0};



Also,

We can initialize a range of index to a particular value in an array. 
#Note:-This is a GNU extension (features not found in ISO standard C). 

Example :
int arr[] = { [0 ... 9] = 7, [10 ... 20] = 6, [30 ... 40] = 5};



is equivalent to 
int arr[] = { 7, 7, 7, 7, ..... 6, 6, 6 ...... 5, 5, 5};

No comments:

Post a Comment