Showing posts with label Array. Show all posts
Showing posts with label Array. Show all posts

27.2.08

Variable length Arrays/Dynamic Arrays

C99 added the feature called dynamic arrays in C.

Means we can specify the length of the array in the run time.

But there is one limitation that once the length of the array specified,

it’s not possible to change the length of array again.


Code fragment.

int length;

int arr[length];

Printf(“Enter the Length of the array \n”);

Scanf (“%d”, &length);



NOTE: Here the length of the arr is length and it should not be changed.

22.2.08

Array Initialization

Here i am discussing how many ways we can initialize the array in
the 'C' language

Normal initialization
int arr[10] = { 0, 1, 2, 3 , 4, 5, 6, 7, 8, 9 };

This is very normal way of initialization of the array.

-->Let say there is a situation that all of the array elements holds the same
element.Different ways to initialize that is

int arr[ 10] = {0,0,0,0,0,0,0,0,0,0,}; /* this is not a good way */
int arr[10] = {0}; /* Good Practice */

--> let say there is another situation where an array of length "10"
which contains some elements up to index 4 ( means 5 elements)
after that the array contains only zeros.

int arr[10] = {1,12,3,4,5,0,0,0,0,0}; /* This is not good practice */
int arr[10] = { 1,12,3,4,5}; /* rest filled with zeros. */

--> Let say there is another strange situation where first 3 elements in array
holds same value and next 2 elements hold the same value.

int arr[10] = {4,4,4,7,7,1,2,3,4,5}; /* This is normal initialization */
int arr[10] = {[0 ... 2]=4,[3 ... 4]=7,1,2,3,4,5}; /* This is good practice */

--> Let say there is another situation where 3rd element holds 90 ,
8th element holds 100 and rest all holds the zeros.

int arr[10] = {0,0,90,0,0,0,0,100,0,0};/* General practice */
int arr[10] = {[2]=90, [7]=100};/* Good practice */